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. It is also a container
38 * subclass, allowing you to display buttons, curves, pixmaps and any other
41 * You can also set many attributes as: border, foreground and background color,
42 * text justification, and more.
44 * The testgtksheet program shows how easy is to create a spreadsheet-like GUI
54 #include <gdk/gdkkeysyms.h>
55 #include <gtk/gtksignal.h>
56 #include <gtk/gtklabel.h>
57 #include <gtk/gtkbutton.h>
58 #include <gtk/gtkadjustment.h>
59 #include <gtk/gtktable.h>
60 #include <gtk/gtkbox.h>
61 #include <gtk/gtkmain.h>
62 #include <gtk/gtktypeutils.h>
63 #include <gtk/gtkentry.h>
64 #include <gtk/gtkcontainer.h>
65 #include <gtk/gtkpixmap.h>
66 #include <pango/pango.h>
67 #include "gtkitementry.h"
69 #include "gtkextra-marshal.h"
70 #include "gsheetmodel.h"
75 GTK_SHEET_IS_FROZEN = 1 << 1,
76 GTK_SHEET_IN_XDRAG = 1 << 2,
77 GTK_SHEET_IN_YDRAG = 1 << 3,
78 GTK_SHEET_IN_DRAG = 1 << 4,
79 GTK_SHEET_IN_SELECTION = 1 << 5,
80 GTK_SHEET_IN_RESIZE = 1 << 6,
81 GTK_SHEET_REDRAW_PENDING = 1 << 7,
84 #define GTK_SHEET_FLAGS(sheet) (GTK_SHEET (sheet)->flags)
85 #define GTK_SHEET_SET_FLAGS(sheet,flag) (GTK_SHEET_FLAGS (sheet) |= (flag))
86 #define GTK_SHEET_UNSET_FLAGS(sheet,flag) (GTK_SHEET_FLAGS (sheet) &= ~ (flag))
88 #define GTK_SHEET_IS_FROZEN(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IS_FROZEN)
89 #define GTK_SHEET_IN_XDRAG(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_XDRAG)
90 #define GTK_SHEET_IN_YDRAG(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_YDRAG)
91 #define GTK_SHEET_IN_DRAG(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_DRAG)
92 #define GTK_SHEET_IN_SELECTION(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_SELECTION)
93 #define GTK_SHEET_IN_RESIZE(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_RESIZE)
94 #define GTK_SHEET_REDRAW_PENDING(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_REDRAW_PENDING)
96 #define CELL_SPACING 1
98 #define TIMEOUT_HOVER 300
99 #define TIME_INTERVAL 8
100 #define COLUMN_MIN_WIDTH 10
105 #define DEFAULT_COLUMN_WIDTH 80
108 static void gtk_sheet_update_primary_selection (GtkSheet *sheet);
109 static void gtk_sheet_column_title_button_draw (GtkSheet *sheet, gint column);
111 static void gtk_sheet_row_title_button_draw (GtkSheet *sheet, gint row);
114 static gboolean gtk_sheet_cell_empty (const GtkSheet *sheet, gint row, gint col);
117 dispose_string (const GtkSheet *sheet, gchar *text)
119 GSheetModel *model = gtk_sheet_get_model (sheet);
124 if (g_sheet_model_free_strings (model))
129 guint DEFAULT_ROW_HEIGHT (GtkWidget *widget)
131 if (!widget->style->font_desc) return 24;
134 PangoContext *context = gtk_widget_get_pango_context (widget);
135 PangoFontMetrics *metrics =
136 pango_context_get_metrics (context,
137 widget->style->font_desc,
138 pango_context_get_language (context));
140 guint val = pango_font_metrics_get_descent (metrics) +
141 pango_font_metrics_get_ascent (metrics);
143 pango_font_metrics_unref (metrics);
145 return PANGO_PIXELS (val) + 2 * CELLOFFSET;
150 guint DEFAULT_FONT_ASCENT (GtkWidget *widget)
152 if (!widget->style->font_desc) return 12;
155 PangoContext *context = gtk_widget_get_pango_context (widget);
156 PangoFontMetrics *metrics =
157 pango_context_get_metrics (context,
158 widget->style->font_desc,
159 pango_context_get_language (context));
160 guint val = pango_font_metrics_get_ascent (metrics);
161 pango_font_metrics_unref (metrics);
162 return PANGO_PIXELS (val);
167 guint STRING_WIDTH (GtkWidget *widget,
168 const PangoFontDescription *font, const gchar *text)
173 layout = gtk_widget_create_pango_layout (widget, text);
174 pango_layout_set_font_description (layout, font);
176 pango_layout_get_extents (layout, NULL, &rect);
178 g_object_unref (layout);
179 return PANGO_PIXELS (rect.width);
183 guint DEFAULT_FONT_DESCENT (GtkWidget *widget)
185 if (!widget->style->font_desc) return 12;
188 PangoContext *context = gtk_widget_get_pango_context (widget);
189 PangoFontMetrics *metrics =
190 pango_context_get_metrics (context,
191 widget->style->font_desc,
192 pango_context_get_language (context));
193 guint val = pango_font_metrics_get_descent (metrics);
194 pango_font_metrics_unref (metrics);
195 return PANGO_PIXELS (val);
201 yyy_row_is_visible (const GtkSheet *sheet, gint row)
203 GSheetRow *row_geo = sheet->row_geometry;
205 return g_sheet_row_get_visibility (row_geo, row, 0);
210 yyy_row_is_sensitive (const GtkSheet *sheet, gint row)
212 GSheetRow *row_geo = sheet->row_geometry;
214 return g_sheet_row_get_sensitivity (row_geo, row, 0);
220 yyy_row_count (const GtkSheet *sheet)
222 GSheetRow *row_geo = sheet->row_geometry;
224 return g_sheet_row_get_row_count (row_geo, 0);
228 yyy_row_height (const GtkSheet *sheet, gint row)
230 GSheetRow *row_geo = sheet->row_geometry;
232 return g_sheet_row_get_height (row_geo, row, 0);
236 yyy_row_top_ypixel (const GtkSheet *sheet, gint row)
238 GSheetRow *geo = sheet->row_geometry;
240 gint y = g_sheet_row_start_pixel (geo, row, 0);
242 if ( sheet->column_titles_visible )
243 y += sheet->column_title_area.height;
249 /* Return the row containing pixel Y */
251 yyy_row_ypixel_to_row (const GtkSheet *sheet, gint y)
253 GSheetRow *geo = sheet->row_geometry;
255 gint cy = sheet->voffset;
257 if (sheet->column_titles_visible)
258 cy += sheet->column_title_area.height;
260 if (y < cy) return 0;
262 return g_sheet_row_pixel_to_row (geo, y - cy, 0);
266 /* gives the top pixel of the given row in context of
267 * the sheet's voffset */
269 ROW_TOP_YPIXEL (const GtkSheet *sheet, gint row)
271 return (sheet->voffset + yyy_row_top_ypixel (sheet, row));
275 /* returns the row index from a y pixel location in the
276 * context of the sheet's voffset */
278 ROW_FROM_YPIXEL (const GtkSheet *sheet, gint y)
280 return (yyy_row_ypixel_to_row (sheet, y));
283 static inline GtkSheetButton *
284 xxx_column_button (const GtkSheet *sheet, gint col)
286 GSheetColumn *col_geo = sheet->column_geometry;
287 if ( col < 0 ) return NULL ;
289 return g_sheet_column_get_button (col_geo, col);
294 xxx_column_left_xpixel (const GtkSheet *sheet, gint col)
296 GSheetColumn *geo = sheet->column_geometry;
298 gint x = g_sheet_column_start_pixel (geo, col);
300 if ( sheet->row_titles_visible )
301 x += sheet->row_title_area.width;
307 xxx_column_width (const GtkSheet *sheet, gint col)
309 GSheetColumn *col_geo = sheet->column_geometry;
311 return g_sheet_column_get_width (col_geo, col);
316 xxx_set_column_width (GtkSheet *sheet, gint col, gint width)
318 if ( sheet->column_geometry )
319 g_sheet_column_set_width (sheet->column_geometry, col, width);
323 xxx_column_set_left_column (GtkSheet *sheet, gint col, gint i)
325 GSheetColumn *col_geo = sheet->column_geometry;
327 g_sheet_column_set_left_text_column (col_geo, col, i);
331 xxx_column_left_column (const GtkSheet *sheet, gint col)
333 GSheetColumn *col_geo = sheet->column_geometry;
335 return g_sheet_column_get_left_text_column (col_geo, col);
339 xxx_column_set_right_column (GtkSheet *sheet, gint col, gint i)
341 GSheetColumn *col_geo = sheet->column_geometry;
343 g_sheet_column_set_right_text_column (col_geo, col, i);
347 xxx_column_right_column (const GtkSheet *sheet, gint col)
349 GSheetColumn *col_geo = sheet->column_geometry;
351 return g_sheet_column_get_right_text_column (col_geo, col);
354 static inline GtkJustification
355 xxx_column_justification (const GtkSheet *sheet, gint col)
357 GSheetColumn *col_geo = sheet->column_geometry;
359 return g_sheet_column_get_justification (col_geo, col);
363 xxx_column_is_visible (const GtkSheet *sheet, gint col)
365 GSheetColumn *col_geo = sheet->column_geometry;
367 return g_sheet_column_get_visibility (col_geo, col);
372 xxx_column_is_sensitive (const GtkSheet *sheet, gint col)
374 GSheetColumn *col_geo = sheet->column_geometry;
376 return g_sheet_column_get_sensitivity (col_geo, col);
380 /* gives the left pixel of the given column in context of
381 * the sheet's hoffset */
383 COLUMN_LEFT_XPIXEL (const GtkSheet *sheet, gint ncol)
385 return (sheet->hoffset + xxx_column_left_xpixel (sheet, ncol));
389 xxx_column_count (const GtkSheet *sheet)
391 GSheetColumn *col_geo = sheet->column_geometry;
393 return g_sheet_column_get_column_count (col_geo);
396 /* returns the column index from a x pixel location in the
397 * context of the sheet's hoffset */
399 COLUMN_FROM_XPIXEL (const GtkSheet * sheet,
405 if ( sheet->row_titles_visible )
406 cx += sheet->row_title_area.width;
408 if (x < cx) return 0;
409 for (i = 0; i < xxx_column_count (sheet); i++)
411 if (x >= cx && x <= (cx + xxx_column_width (sheet, i)) &&
412 xxx_column_is_visible (sheet, i))
414 if ( xxx_column_is_visible (sheet, i))
415 cx += xxx_column_width (sheet, i);
419 return xxx_column_count (sheet) - 1;
422 /* returns the total height of the sheet */
423 static inline gint SHEET_HEIGHT (GtkSheet *sheet)
425 const gint n_rows = yyy_row_count (sheet);
427 return yyy_row_top_ypixel (sheet, n_rows - 1) +
428 yyy_row_height (sheet, n_rows - 1);
432 static inline GtkSheetButton *
433 yyy_row_button (GtkSheet *sheet, gint row)
435 GSheetRow *row_geo = sheet->row_geometry;
437 return g_sheet_row_get_button (row_geo, row, sheet);
444 yyy_set_row_height (GtkSheet *sheet, gint row, gint height)
446 if ( sheet->row_geometry )
447 g_sheet_row_set_height (sheet->row_geometry, row, height, sheet);
452 /* returns the total width of the sheet */
453 static inline gint SHEET_WIDTH (GtkSheet *sheet)
457 cx = ( sheet->row_titles_visible ? sheet->row_title_area.width : 0);
459 for (i = 0; i < xxx_column_count (sheet); i++)
460 if (xxx_column_is_visible (sheet, i))
461 cx += xxx_column_width (sheet, i);
466 #define MIN_VISIBLE_ROW(sheet) \
467 ROW_FROM_YPIXEL (sheet, sheet->column_title_area.height + 1)
469 #define MAX_VISIBLE_ROW(sheet) \
470 ROW_FROM_YPIXEL (sheet, sheet->sheet_window_height - 1)
472 #define MIN_VISIBLE_COLUMN(sheet) \
473 COLUMN_FROM_XPIXEL (sheet, sheet->row_title_area.width + 1)
475 #define MAX_VISIBLE_COLUMN(sheet) \
476 COLUMN_FROM_XPIXEL (sheet, sheet->sheet_window_width)
480 static inline gboolean
481 POSSIBLE_XDRAG (const GtkSheet *sheet, gint x, gint *drag_column)
485 column = COLUMN_FROM_XPIXEL (sheet, x);
486 *drag_column = column;
488 xdrag = COLUMN_LEFT_XPIXEL (sheet, column) + CELL_SPACING;
489 if (x <= xdrag + DRAG_WIDTH / 2 && column != 0)
491 while (! xxx_column_is_visible (sheet, column - 1) && column > 0) column--;
492 *drag_column = column - 1;
493 return xxx_column_is_sensitive (sheet, column - 1);
496 xdrag += xxx_column_width (sheet, column);
497 if (x >= xdrag - DRAG_WIDTH / 2 && x <= xdrag + DRAG_WIDTH / 2)
498 return xxx_column_is_sensitive (sheet, column);
503 static inline gboolean
504 POSSIBLE_YDRAG (const GtkSheet *sheet, gint y, gint *drag_row)
507 row = ROW_FROM_YPIXEL (sheet, y);
510 ydrag = ROW_TOP_YPIXEL (sheet, row)+CELL_SPACING;
511 if (y <= ydrag + DRAG_WIDTH / 2 && row != 0)
513 while (!yyy_row_is_visible (sheet, row - 1) && row > 0) row--;
515 return yyy_row_is_sensitive (sheet, row - 1);
518 ydrag +=yyy_row_height (sheet, row);
520 if (y >= ydrag - DRAG_WIDTH / 2 && y <= ydrag + DRAG_WIDTH / 2)
521 return yyy_row_is_sensitive (sheet, row);
527 static inline gboolean
528 POSSIBLE_DRAG (const GtkSheet *sheet, gint x, gint y,
529 gint *drag_row, gint *drag_column)
533 /* Can't drag if nothing is selected */
534 if ( sheet->range.row0 < 0 || sheet->range.rowi < 0 ||
535 sheet->range.col0 < 0 || sheet->range.coli < 0 )
538 *drag_column = COLUMN_FROM_XPIXEL (sheet, x);
539 *drag_row = ROW_FROM_YPIXEL (sheet, y);
541 if (x >= COLUMN_LEFT_XPIXEL (sheet, sheet->range.col0) - DRAG_WIDTH / 2 &&
542 x <= COLUMN_LEFT_XPIXEL (sheet, sheet->range.coli) +
543 xxx_column_width (sheet, sheet->range.coli) + DRAG_WIDTH / 2)
545 ydrag = ROW_TOP_YPIXEL (sheet, sheet->range.row0);
546 if (y >= ydrag - DRAG_WIDTH / 2 && y <= ydrag + DRAG_WIDTH / 2)
548 *drag_row = sheet->range.row0;
551 ydrag = ROW_TOP_YPIXEL (sheet, sheet->range.rowi) +
552 yyy_row_height (sheet, sheet->range.rowi);
553 if (y >= ydrag - DRAG_WIDTH / 2 && y <= ydrag + DRAG_WIDTH / 2)
555 *drag_row = sheet->range.rowi;
560 if (y >= ROW_TOP_YPIXEL (sheet, sheet->range.row0) - DRAG_WIDTH / 2 &&
561 y <= ROW_TOP_YPIXEL (sheet, sheet->range.rowi) +
562 yyy_row_height (sheet, sheet->range.rowi) + DRAG_WIDTH / 2)
564 xdrag = COLUMN_LEFT_XPIXEL (sheet, sheet->range.col0);
565 if (x >= xdrag - DRAG_WIDTH / 2 && x <= xdrag + DRAG_WIDTH / 2)
567 *drag_column = sheet->range.col0;
570 xdrag = COLUMN_LEFT_XPIXEL (sheet, sheet->range.coli) +
571 xxx_column_width (sheet, sheet->range.coli);
572 if (x >= xdrag - DRAG_WIDTH / 2 && x <= xdrag + DRAG_WIDTH / 2)
574 *drag_column = sheet->range.coli;
582 static inline gboolean
583 POSSIBLE_RESIZE (const GtkSheet *sheet, gint x, gint y,
584 gint *drag_row, gint *drag_column)
588 /* Can't drag if nothing is selected */
589 if ( sheet->range.row0 < 0 || sheet->range.rowi < 0 ||
590 sheet->range.col0 < 0 || sheet->range.coli < 0 )
593 xdrag = COLUMN_LEFT_XPIXEL (sheet, sheet->range.coli)+
594 xxx_column_width (sheet, sheet->range.coli);
596 ydrag = ROW_TOP_YPIXEL (sheet, sheet->range.rowi)+
597 yyy_row_height (sheet, sheet->range.rowi);
599 if (sheet->state == GTK_SHEET_COLUMN_SELECTED)
600 ydrag = ROW_TOP_YPIXEL (sheet, MIN_VISIBLE_ROW (sheet));
602 if (sheet->state == GTK_SHEET_ROW_SELECTED)
603 xdrag = COLUMN_LEFT_XPIXEL (sheet, MIN_VISIBLE_COLUMN (sheet));
605 *drag_column = COLUMN_FROM_XPIXEL (sheet, x);
606 *drag_row = ROW_FROM_YPIXEL (sheet, y);
608 if (x >= xdrag - DRAG_WIDTH / 2 && x <= xdrag + DRAG_WIDTH / 2 &&
609 y >= ydrag - DRAG_WIDTH / 2 && y <= ydrag + DRAG_WIDTH / 2) return TRUE;
614 static void gtk_sheet_class_init (GtkSheetClass * klass);
615 static void gtk_sheet_init (GtkSheet * sheet);
616 static void gtk_sheet_dispose (GObject * object);
617 static void gtk_sheet_finalize (GObject * object);
618 static void gtk_sheet_style_set (GtkWidget *widget,
619 GtkStyle *previous_style);
620 static void gtk_sheet_realize (GtkWidget * widget);
621 static void gtk_sheet_unrealize (GtkWidget * widget);
622 static void gtk_sheet_map (GtkWidget * widget);
623 static void gtk_sheet_unmap (GtkWidget * widget);
624 static gint gtk_sheet_expose (GtkWidget * widget,
625 GdkEventExpose * event);
626 static void gtk_sheet_forall (GtkContainer *container,
627 gboolean include_internals,
628 GtkCallback callback,
629 gpointer callback_data);
631 static void gtk_sheet_set_scroll_adjustments (GtkSheet *sheet,
632 GtkAdjustment *hadjustment,
633 GtkAdjustment *vadjustment);
635 static gint gtk_sheet_button_press (GtkWidget * widget,
636 GdkEventButton * event);
637 static gint gtk_sheet_button_release (GtkWidget * widget,
638 GdkEventButton * event);
639 static gint gtk_sheet_motion (GtkWidget * widget,
640 GdkEventMotion * event);
641 static gint gtk_sheet_entry_key_press (GtkWidget *widget,
643 static gint gtk_sheet_key_press (GtkWidget *widget,
645 static void gtk_sheet_size_request (GtkWidget * widget,
646 GtkRequisition * requisition);
647 static void gtk_sheet_size_allocate (GtkWidget * widget,
648 GtkAllocation * allocation);
652 static gboolean gtk_sheet_range_isvisible (const GtkSheet * sheet,
653 GtkSheetRange range);
654 static gboolean gtk_sheet_cell_isvisible (GtkSheet * sheet,
655 gint row, gint column);
656 /* Drawing Routines */
658 /* draw cell background and frame */
659 static void gtk_sheet_cell_draw_default (GtkSheet *sheet,
660 gint row, gint column);
662 /* draw cell contents */
663 static void gtk_sheet_cell_draw_label (GtkSheet *sheet,
664 gint row, gint column);
666 /* draw visible part of range. If range == NULL then draw the whole screen */
667 static void gtk_sheet_range_draw (GtkSheet *sheet,
668 const GtkSheetRange *range);
670 /* highlight the visible part of the selected range */
671 static void gtk_sheet_range_draw_selection (GtkSheet *sheet,
672 GtkSheetRange range);
676 static gboolean gtk_sheet_move_query (GtkSheet *sheet,
677 gint row, gint column);
678 static void gtk_sheet_real_select_range (GtkSheet * sheet,
679 const GtkSheetRange * range);
680 static void gtk_sheet_real_unselect_range (GtkSheet * sheet,
681 const GtkSheetRange * range);
682 static void gtk_sheet_extend_selection (GtkSheet *sheet,
683 gint row, gint column);
684 static void gtk_sheet_new_selection (GtkSheet *sheet,
685 GtkSheetRange *range);
686 static void gtk_sheet_draw_border (GtkSheet *sheet,
687 GtkSheetRange range);
688 static void gtk_sheet_draw_corners (GtkSheet *sheet,
689 GtkSheetRange range);
692 /* Active Cell handling */
694 static void gtk_sheet_entry_changed (GtkWidget *widget,
696 static void gtk_sheet_deactivate_cell (GtkSheet *sheet);
697 static void gtk_sheet_hide_active_cell (GtkSheet *sheet);
698 static gboolean gtk_sheet_activate_cell (GtkSheet *sheet,
700 static void gtk_sheet_draw_active_cell (GtkSheet *sheet);
701 static void gtk_sheet_show_active_cell (GtkSheet *sheet);
702 static void gtk_sheet_click_cell (GtkSheet *sheet,
709 static void gtk_sheet_make_backing_pixmap (GtkSheet *sheet,
710 guint width, guint height);
711 static void gtk_sheet_draw_backing_pixmap (GtkSheet *sheet,
712 GtkSheetRange range);
715 static void adjust_scrollbars (GtkSheet * sheet);
716 static void vadjustment_value_changed (GtkAdjustment * adjustment,
718 static void hadjustment_value_changed (GtkAdjustment * adjustment,
722 static void draw_xor_vline (GtkSheet * sheet);
723 static void draw_xor_hline (GtkSheet * sheet);
724 static void draw_xor_rectangle (GtkSheet *sheet,
725 GtkSheetRange range);
727 static guint new_column_width (GtkSheet * sheet,
730 static guint new_row_height (GtkSheet * sheet,
735 static void create_global_button (GtkSheet *sheet);
736 static void global_button_clicked (GtkWidget *widget,
740 static void create_sheet_entry (GtkSheet *sheet);
741 static void gtk_sheet_size_allocate_entry (GtkSheet *sheet);
742 static void gtk_sheet_entry_set_max_size (GtkSheet *sheet);
744 /* Sheet button gadgets */
746 static void size_allocate_column_title_buttons (GtkSheet * sheet);
747 static void size_allocate_row_title_buttons (GtkSheet * sheet);
750 static void size_allocate_global_button (GtkSheet *sheet);
751 static void gtk_sheet_button_size_request (GtkSheet *sheet,
752 const GtkSheetButton *button,
753 GtkRequisition *requisition);
755 /* Attributes routines */
756 static void init_attributes (const GtkSheet *sheet, gint col,
757 GtkSheetCellAttr *attributes);
760 /* Memory allocation routines */
761 static void gtk_sheet_real_range_clear (GtkSheet *sheet,
762 const GtkSheetRange *range);
764 static void gtk_sheet_real_cell_clear (GtkSheet *sheet,
769 /* Container Functions */
770 static void gtk_sheet_remove (GtkContainer *container,
772 static void gtk_sheet_realize_child (GtkSheet *sheet,
773 GtkSheetChild *child);
774 static void gtk_sheet_position_child (GtkSheet *sheet,
775 GtkSheetChild *child);
776 static void gtk_sheet_position_children (GtkSheet *sheet);
777 static void gtk_sheet_child_show (GtkSheetChild *child);
778 static void gtk_sheet_child_hide (GtkSheetChild *child);
779 static void gtk_sheet_column_size_request (GtkSheet *sheet,
782 static void gtk_sheet_row_size_request (GtkSheet *sheet,
790 _gtkextra_signal_emit (GtkObject *object, guint signal_id, ...);
810 static GtkContainerClass *parent_class = NULL;
811 static guint sheet_signals[LAST_SIGNAL] = { 0 };
815 gtk_sheet_get_type ()
817 static GType sheet_type = 0;
821 static const GTypeInfo sheet_info =
823 sizeof (GtkSheetClass),
826 (GClassInitFunc) gtk_sheet_class_init,
831 (GInstanceInitFunc) gtk_sheet_init,
835 g_type_register_static (GTK_TYPE_CONTAINER, "GtkSheet",
841 static GtkSheetRange*
842 gtk_sheet_range_copy (const GtkSheetRange *range)
844 GtkSheetRange *new_range;
846 g_return_val_if_fail (range != NULL, NULL);
848 new_range = g_new (GtkSheetRange, 1);
856 gtk_sheet_range_free (GtkSheetRange *range)
858 g_return_if_fail (range != NULL);
864 gtk_sheet_range_get_type (void)
866 static GType sheet_range_type = 0;
868 if (!sheet_range_type)
871 g_boxed_type_register_static ("GtkSheetRange",
872 (GBoxedCopyFunc) gtk_sheet_range_copy,
873 (GBoxedFreeFunc) gtk_sheet_range_free);
876 return sheet_range_type;
880 static void column_titles_changed (GtkWidget *w, gint first, gint n_columns, gpointer data);
892 gtk_sheet_set_property (GObject *object,
898 GtkSheet *sheet = GTK_SHEET (object);
903 sheet->row_geometry = g_value_get_pointer (value);
906 sheet->column_geometry = g_value_get_pointer (value);
907 if ( sheet->column_geometry)
908 g_signal_connect (sheet->column_geometry, "columns_changed",
909 G_CALLBACK (column_titles_changed), sheet);
912 gtk_sheet_set_model (sheet, g_value_get_pointer (value));
915 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
921 gtk_sheet_get_property (GObject *object,
926 GtkSheet *sheet = GTK_SHEET (object);
931 g_value_set_pointer (value, sheet->row_geometry);
934 g_value_set_pointer (value, sheet->column_geometry);
937 g_value_set_pointer (value, sheet->model);
940 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
947 gtk_sheet_class_init (GtkSheetClass * klass)
949 GObjectClass *object_class = G_OBJECT_CLASS (klass);
951 GParamSpec *row_geo_spec ;
952 GParamSpec *col_geo_spec ;
953 GParamSpec *model_spec ;
955 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
956 GtkContainerClass *container_class = (GtkContainerClass *) klass;
958 parent_class = g_type_class_peek_parent (klass);
961 * GtkSheet::select-row
962 * @sheet: the sheet widget that emitted the signal
963 * @row: the newly selected row index
965 * A row has been selected.
967 sheet_signals[SELECT_ROW] =
968 g_signal_new ("select-row",
969 G_TYPE_FROM_CLASS (object_class),
971 offsetof (GtkSheetClass, select_row),
973 g_cclosure_marshal_VOID__INT,
980 * GtkSheet::select - column
981 * @sheet: the sheet widget that emitted the signal
982 * @column: the newly selected column index
984 * A column has been selected.
986 sheet_signals[SELECT_COLUMN] =
987 g_signal_new ("select-column",
988 G_TYPE_FROM_CLASS (object_class),
990 offsetof (GtkSheetClass, select_column),
992 g_cclosure_marshal_VOID__INT,
999 * GtkSheet::double-click-row
1000 * @sheet: the sheet widget that emitted the signal
1001 * @row: the row that was double clicked.
1003 * A row's title button has been double clicked
1005 sheet_signals[DOUBLE_CLICK_ROW] =
1006 g_signal_new ("double-click-row",
1007 G_TYPE_FROM_CLASS (object_class),
1011 g_cclosure_marshal_VOID__INT,
1018 * GtkSheet::double-click-column
1019 * @sheet: the sheet widget that emitted the signal
1020 * @column: the column that was double clicked.
1022 * A column's title button has been double clicked
1024 sheet_signals[DOUBLE_CLICK_COLUMN] =
1025 g_signal_new ("double-click-column",
1026 G_TYPE_FROM_CLASS (object_class),
1030 g_cclosure_marshal_VOID__INT,
1037 * GtkSheet::button-event-column
1038 * @sheet: the sheet widget that emitted the signal
1039 * @column: the column on which the event occured.
1041 * A button event occured on a column title button
1043 sheet_signals[BUTTON_EVENT_COLUMN] =
1044 g_signal_new ("button-event-column",
1045 G_TYPE_FROM_CLASS (object_class),
1049 gtkextra_VOID__INT_POINTER,
1058 * GtkSheet::button-event-row
1059 * @sheet: the sheet widget that emitted the signal
1060 * @column: the column on which the event occured.
1062 * A button event occured on a row title button
1064 sheet_signals[BUTTON_EVENT_ROW] =
1065 g_signal_new ("button-event-row",
1066 G_TYPE_FROM_CLASS (object_class),
1070 gtkextra_VOID__INT_POINTER,
1078 sheet_signals[SELECT_RANGE] =
1079 g_signal_new ("select-range",
1080 G_TYPE_FROM_CLASS (object_class),
1082 offsetof (GtkSheetClass, select_range),
1084 g_cclosure_marshal_VOID__BOXED,
1087 GTK_TYPE_SHEET_RANGE);
1090 sheet_signals[RESIZE_RANGE] =
1091 g_signal_new ("resize-range",
1092 G_TYPE_FROM_CLASS (object_class),
1094 offsetof (GtkSheetClass, resize_range),
1096 gtkextra_VOID__BOXED_BOXED,
1099 GTK_TYPE_SHEET_RANGE, GTK_TYPE_SHEET_RANGE
1102 sheet_signals[MOVE_RANGE] =
1103 g_signal_new ("move-range",
1104 G_TYPE_FROM_CLASS (object_class),
1106 offsetof (GtkSheetClass, move_range),
1108 gtkextra_VOID__BOXED_BOXED,
1111 GTK_TYPE_SHEET_RANGE, GTK_TYPE_SHEET_RANGE
1114 sheet_signals[TRAVERSE] =
1115 g_signal_new ("traverse",
1116 G_TYPE_FROM_CLASS (object_class),
1118 offsetof (GtkSheetClass, traverse),
1120 gtkextra_BOOLEAN__INT_INT_POINTER_POINTER,
1121 G_TYPE_BOOLEAN, 4, G_TYPE_INT, G_TYPE_INT,
1122 G_TYPE_POINTER, G_TYPE_POINTER);
1125 sheet_signals[DEACTIVATE] =
1126 g_signal_new ("deactivate",
1127 G_TYPE_FROM_CLASS (object_class),
1129 offsetof (GtkSheetClass, deactivate),
1131 gtkextra_VOID__INT_INT,
1132 G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
1134 sheet_signals[ACTIVATE] =
1135 g_signal_new ("activate",
1136 G_TYPE_FROM_CLASS (object_class),
1138 offsetof (GtkSheetClass, activate),
1140 gtkextra_BOOLEAN__INT_INT,
1141 G_TYPE_BOOLEAN, 2, G_TYPE_INT, G_TYPE_INT);
1143 sheet_signals[CHANGED] =
1144 g_signal_new ("changed",
1145 G_TYPE_FROM_CLASS (object_class),
1147 offsetof (GtkSheetClass, changed),
1149 gtkextra_VOID__INT_INT,
1150 G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
1152 widget_class->set_scroll_adjustments_signal =
1153 g_signal_new ("set-scroll-adjustments",
1154 G_TYPE_FROM_CLASS (object_class),
1156 offsetof (GtkSheetClass, set_scroll_adjustments),
1158 gtkextra_VOID__OBJECT_OBJECT,
1159 G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
1162 container_class->add = NULL;
1163 container_class->remove = gtk_sheet_remove;
1164 container_class->forall = gtk_sheet_forall;
1166 object_class->dispose = gtk_sheet_dispose;
1167 object_class->finalize = gtk_sheet_finalize;
1171 g_param_spec_pointer ("row-geometry",
1173 "A pointer to the model of the row geometry",
1174 G_PARAM_READABLE | G_PARAM_WRITABLE );
1177 g_param_spec_pointer ("column-geometry",
1179 "A pointer to the model of the column geometry",
1180 G_PARAM_READABLE | G_PARAM_WRITABLE );
1183 g_param_spec_pointer ("model",
1185 "A pointer to the data model",
1186 G_PARAM_READABLE | G_PARAM_WRITABLE );
1189 object_class->set_property = gtk_sheet_set_property;
1190 object_class->get_property = gtk_sheet_get_property;
1192 g_object_class_install_property (object_class,
1196 g_object_class_install_property (object_class,
1200 g_object_class_install_property (object_class,
1205 widget_class->realize = gtk_sheet_realize;
1206 widget_class->unrealize = gtk_sheet_unrealize;
1207 widget_class->map = gtk_sheet_map;
1208 widget_class->unmap = gtk_sheet_unmap;
1209 widget_class->style_set = gtk_sheet_style_set;
1210 widget_class->button_press_event = gtk_sheet_button_press;
1211 widget_class->button_release_event = gtk_sheet_button_release;
1212 widget_class->motion_notify_event = gtk_sheet_motion;
1213 widget_class->key_press_event = gtk_sheet_key_press;
1214 widget_class->expose_event = gtk_sheet_expose;
1215 widget_class->size_request = gtk_sheet_size_request;
1216 widget_class->size_allocate = gtk_sheet_size_allocate;
1217 widget_class->focus_in_event = NULL;
1218 widget_class->focus_out_event = NULL;
1220 klass->set_scroll_adjustments = gtk_sheet_set_scroll_adjustments;
1221 klass->select_row = NULL;
1222 klass->select_column = NULL;
1223 klass->select_range = NULL;
1224 klass->resize_range = NULL;
1225 klass->move_range = NULL;
1226 klass->traverse = NULL;
1227 klass->deactivate = NULL;
1228 klass->activate = NULL;
1229 klass->changed = NULL;
1233 gtk_sheet_init (GtkSheet *sheet)
1235 sheet->column_geometry = NULL;
1236 sheet->row_geometry = NULL;
1238 sheet->children = NULL;
1241 sheet->selection_mode = GTK_SELECTION_NONE;
1242 sheet->freeze_count = 0;
1243 sheet->state = GTK_SHEET_NORMAL;
1245 GTK_WIDGET_UNSET_FLAGS (sheet, GTK_NO_WINDOW);
1246 GTK_WIDGET_SET_FLAGS (sheet, GTK_CAN_FOCUS);
1248 sheet->column_title_window = NULL;
1249 sheet->column_title_area.x = 0;
1250 sheet->column_title_area.y = 0;
1251 sheet->column_title_area.width = 0;
1252 sheet->column_title_area.height = DEFAULT_ROW_HEIGHT (GTK_WIDGET (sheet));
1254 sheet->row_title_window = NULL;
1255 sheet->row_title_area.x = 0;
1256 sheet->row_title_area.y = 0;
1257 sheet->row_title_area.width = DEFAULT_COLUMN_WIDTH;
1258 sheet->row_title_area.height = 0;
1261 sheet->active_cell.row = 0;
1262 sheet->active_cell.col = 0;
1263 sheet->selection_cell.row = 0;
1264 sheet->selection_cell.col = 0;
1266 sheet->pixmap = NULL;
1268 sheet->range.row0 = 0;
1269 sheet->range.rowi = 0;
1270 sheet->range.col0 = 0;
1271 sheet->range.coli = 0;
1273 sheet->state = GTK_SHEET_NORMAL;
1275 sheet->sheet_window = NULL;
1276 sheet->sheet_window_width = 0;
1277 sheet->sheet_window_height = 0;
1278 sheet->entry_widget = NULL;
1279 sheet->entry_container = NULL;
1280 sheet->button = NULL;
1285 sheet->hadjustment = NULL;
1286 sheet->vadjustment = NULL;
1288 sheet->cursor_drag = gdk_cursor_new (GDK_PLUS);
1289 sheet->xor_gc = NULL;
1290 sheet->fg_gc = NULL;
1291 sheet->bg_gc = NULL;
1295 gdk_color_parse ("white", &sheet->bg_color);
1296 gdk_color_alloc (gdk_colormap_get_system (), &sheet->bg_color);
1297 gdk_color_parse ("gray", &sheet->grid_color);
1298 gdk_color_alloc (gdk_colormap_get_system (), &sheet->grid_color);
1299 sheet->show_grid = TRUE;
1301 sheet->motion_timer = 0;
1303 sheet->columns_resizable = TRUE;
1304 sheet->rows_resizable = TRUE;
1306 sheet->row_titles_visible = TRUE;
1307 sheet->row_title_area.width = DEFAULT_COLUMN_WIDTH;
1309 sheet->column_titles_visible = TRUE;
1310 sheet->autoscroll = TRUE;
1311 sheet->justify_entry = TRUE;
1314 /* create sheet entry */
1315 sheet->entry_type = 0;
1316 create_sheet_entry (sheet);
1318 /* create global selection button */
1319 create_global_button (sheet);
1323 /* Callback which occurs whenever columns are inserted / deleted in the model */
1325 columns_inserted_deleted_callback (GSheetModel *model, gint first_column,
1330 GtkSheet *sheet = GTK_SHEET (data);
1332 GtkSheetRange range;
1333 gint model_columns = g_sheet_model_get_column_count (model);
1336 /* Need to update all the columns starting from the first column and onwards.
1337 * Previous column are unchanged, so don't need to be updated.
1339 range.col0 = first_column;
1341 range.coli = xxx_column_count (sheet) - 1;
1342 range.rowi = yyy_row_count (sheet) - 1;
1344 adjust_scrollbars (sheet);
1346 if (sheet->active_cell.col >= model_columns)
1347 gtk_sheet_activate_cell (sheet, sheet->active_cell.row, model_columns - 1);
1349 for (i = first_column; i <= MAX_VISIBLE_COLUMN (sheet); i++)
1350 gtk_sheet_column_title_button_draw (sheet, i);
1352 gtk_sheet_range_draw (sheet, &range);
1356 /* Callback which occurs whenever rows are inserted / deleted in the model */
1358 rows_inserted_deleted_callback (GSheetModel *model, gint first_row,
1359 gint n_rows, gpointer data)
1362 GtkSheet *sheet = GTK_SHEET (data);
1364 GtkSheetRange range;
1366 gint model_rows = g_sheet_model_get_row_count (model);
1368 /* Need to update all the rows starting from the first row and onwards.
1369 * Previous rows are unchanged, so don't need to be updated.
1371 range.row0 = first_row;
1373 range.rowi = yyy_row_count (sheet) - 1;
1374 range.coli = xxx_column_count (sheet) - 1;
1376 adjust_scrollbars (sheet);
1378 if (sheet->active_cell.row >= model_rows)
1379 gtk_sheet_activate_cell (sheet, model_rows - 1, sheet->active_cell.col);
1381 for (i = first_row; i <= MAX_VISIBLE_ROW (sheet); i++)
1382 gtk_sheet_row_title_button_draw (sheet, i);
1384 gtk_sheet_range_draw (sheet, &range);
1388 If row0 or rowi are negative, then all rows will be updated.
1389 If col0 or coli are negative, then all columns will be updated.
1392 range_update_callback (GSheetModel *m, gint row0, gint col0,
1393 gint rowi, gint coli, gpointer data)
1395 GtkSheet *sheet = GTK_SHEET (data);
1397 GtkSheetRange range;
1404 if ( MAX_VISIBLE_ROW (sheet) >
1405 g_sheet_model_get_row_count (sheet->model)
1407 MAX_VISIBLE_COLUMN (sheet) >
1408 g_sheet_model_get_column_count (sheet->model))
1410 gtk_sheet_move_query (sheet, 0, 0);
1413 if ( ( row0 < 0 && col0 < 0 ) || ( rowi < 0 && coli < 0 ) )
1416 gtk_sheet_range_draw (sheet, NULL);
1417 adjust_scrollbars (sheet);
1419 for (i = MIN_VISIBLE_ROW (sheet); i <= MAX_VISIBLE_ROW (sheet); i++)
1420 gtk_sheet_row_title_button_draw (sheet, i);
1422 for (i = MIN_VISIBLE_COLUMN (sheet);
1423 i <= MAX_VISIBLE_COLUMN (sheet); i++)
1424 gtk_sheet_column_title_button_draw (sheet, i);
1428 else if ( row0 < 0 || rowi < 0 )
1430 range.row0 = MIN_VISIBLE_ROW (sheet);
1431 range.rowi = MAX_VISIBLE_ROW (sheet);
1433 else if ( col0 < 0 || coli < 0 )
1435 range.col0 = MIN_VISIBLE_COLUMN (sheet);
1436 range.coli = MAX_VISIBLE_COLUMN (sheet);
1439 gtk_sheet_range_draw (sheet, &range);
1445 * @rows: initial number of rows
1446 * @columns: initial number of columns
1447 * @title: sheet title
1448 * @model: the model to use for the sheet data
1450 * Creates a new sheet widget with the given number of rows and columns.
1452 * Returns: the new sheet widget
1455 gtk_sheet_new (GSheetRow *vgeo, GSheetColumn *hgeo, GSheetModel *model)
1457 GtkWidget *widget = g_object_new (GTK_TYPE_SHEET,
1458 "row-geometry", vgeo,
1459 "column-geometry", hgeo,
1467 * gtk_sheet_set_model
1468 * @sheet: the sheet to set the model for
1469 * @model: the model to use for the sheet data
1471 * Sets the model for a GtkSheet
1475 gtk_sheet_set_model (GtkSheet *sheet, GSheetModel *model)
1477 g_return_if_fail (GTK_IS_SHEET (sheet));
1479 if (sheet->model ) g_object_unref (sheet->model);
1481 sheet->model = model;
1485 g_signal_connect (model, "range_changed",
1486 G_CALLBACK (range_update_callback), sheet);
1488 g_signal_connect (model, "rows_inserted",
1489 G_CALLBACK (rows_inserted_deleted_callback), sheet);
1491 g_signal_connect (model, "rows_deleted",
1492 G_CALLBACK (rows_inserted_deleted_callback), sheet);
1494 g_signal_connect (model, "columns_inserted",
1495 G_CALLBACK (columns_inserted_deleted_callback), sheet);
1497 g_signal_connect (model, "columns_deleted",
1498 G_CALLBACK (columns_inserted_deleted_callback), sheet);
1503 /* Call back for when the column titles have changed.
1504 FIRST is the first column changed.
1505 N_COLUMNS is the number of columns which have changed, or - 1, which
1506 indicates that the column has changed to its right - most extremity
1509 column_titles_changed (GtkWidget *w, gint first, gint n_columns, gpointer data)
1511 GtkSheet *sheet = GTK_SHEET (data);
1512 gboolean extremity = FALSE;
1514 if ( n_columns == -1 )
1517 n_columns = xxx_column_count (sheet) - 1 ;
1520 if (!GTK_SHEET_IS_FROZEN (sheet))
1523 for ( i = first ; i <= first + n_columns ; ++i )
1525 gtk_sheet_column_title_button_draw (sheet, i);
1526 g_signal_emit (sheet, sheet_signals[CHANGED], 0, -1, i);
1531 gtk_sheet_column_title_button_draw (sheet, -1);
1536 gtk_sheet_change_entry (GtkSheet *sheet, GtkType entry_type)
1540 g_return_if_fail (sheet != NULL);
1541 g_return_if_fail (GTK_IS_SHEET (sheet));
1543 state = sheet->state;
1545 if (sheet->state == GTK_SHEET_NORMAL)
1546 gtk_sheet_hide_active_cell (sheet);
1548 sheet->entry_type = entry_type;
1550 create_sheet_entry (sheet);
1552 if (state == GTK_SHEET_NORMAL)
1554 gtk_sheet_show_active_cell (sheet);
1555 g_signal_connect (gtk_sheet_get_entry (sheet),
1557 G_CALLBACK (gtk_sheet_entry_changed),
1563 gtk_sheet_show_grid (GtkSheet *sheet, gboolean show)
1565 g_return_if_fail (sheet != NULL);
1566 g_return_if_fail (GTK_IS_SHEET (sheet));
1568 if (show == sheet->show_grid) return;
1570 sheet->show_grid = show;
1572 if (!GTK_SHEET_IS_FROZEN (sheet))
1573 gtk_sheet_range_draw (sheet, NULL);
1577 gtk_sheet_grid_visible (GtkSheet *sheet)
1579 g_return_val_if_fail (sheet != NULL, 0);
1580 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
1582 return sheet->show_grid;
1586 gtk_sheet_set_background (GtkSheet *sheet, GdkColor *color)
1588 g_return_if_fail (sheet != NULL);
1589 g_return_if_fail (GTK_IS_SHEET (sheet));
1593 gdk_color_parse ("white", &sheet->bg_color);
1594 gdk_color_alloc (gdk_colormap_get_system (), &sheet->bg_color);
1597 sheet->bg_color = *color;
1599 if (!GTK_SHEET_IS_FROZEN (sheet))
1600 gtk_sheet_range_draw (sheet, NULL);
1604 gtk_sheet_set_grid (GtkSheet *sheet, GdkColor *color)
1606 g_return_if_fail (sheet != NULL);
1607 g_return_if_fail (GTK_IS_SHEET (sheet));
1611 gdk_color_parse ("black", &sheet->grid_color);
1612 gdk_color_alloc (gdk_colormap_get_system (), &sheet->grid_color);
1615 sheet->grid_color = *color;
1617 if (!GTK_SHEET_IS_FROZEN (sheet))
1618 gtk_sheet_range_draw (sheet, NULL);
1622 gtk_sheet_get_columns_count (GtkSheet *sheet)
1624 g_return_val_if_fail (sheet != NULL, 0);
1625 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
1627 return xxx_column_count (sheet);
1631 gtk_sheet_get_rows_count (GtkSheet *sheet)
1633 g_return_val_if_fail (sheet != NULL, 0);
1634 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
1636 return yyy_row_count (sheet);
1640 gtk_sheet_get_state (GtkSheet *sheet)
1642 g_return_val_if_fail (sheet != NULL, 0);
1643 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
1645 return (sheet->state);
1649 gtk_sheet_set_selection_mode (GtkSheet *sheet, gint mode)
1651 g_return_if_fail (sheet != NULL);
1652 g_return_if_fail (GTK_IS_SHEET (sheet));
1654 if (GTK_WIDGET_REALIZED (sheet))
1655 gtk_sheet_real_unselect_range (sheet, NULL);
1657 sheet->selection_mode = mode;
1661 gtk_sheet_set_autoresize (GtkSheet *sheet, gboolean autoresize)
1663 g_return_if_fail (sheet != NULL);
1664 g_return_if_fail (GTK_IS_SHEET (sheet));
1666 sheet->autoresize = autoresize;
1670 gtk_sheet_autoresize (GtkSheet *sheet)
1672 g_return_val_if_fail (sheet != NULL, FALSE);
1673 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
1675 return sheet->autoresize;
1679 gtk_sheet_set_column_width (GtkSheet * sheet,
1685 gtk_sheet_autoresize_column (GtkSheet *sheet, gint column)
1687 gint text_width = 0;
1690 g_return_if_fail (sheet != NULL);
1691 g_return_if_fail (GTK_IS_SHEET (sheet));
1692 if (column >= xxx_column_count (sheet) || column < 0) return;
1694 for (row = 0; row < yyy_row_count (sheet); row++)
1696 gchar *text = gtk_sheet_cell_get_text (sheet, row, column);
1697 if (text && strlen (text) > 0)
1699 GtkSheetCellAttr attributes;
1701 gtk_sheet_get_attributes (sheet, row, column, &attributes);
1702 if (attributes.is_visible)
1704 gint width = STRING_WIDTH (GTK_WIDGET (sheet),
1705 attributes.font_desc,
1707 + 2 * CELLOFFSET + attributes.border.width;
1708 text_width = MAX (text_width, width);
1711 dispose_string (sheet, text);
1714 if (text_width > xxx_column_width (sheet, column) )
1716 gtk_sheet_set_column_width (sheet, column, text_width);
1717 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_REDRAW_PENDING);
1723 gtk_sheet_set_autoscroll (GtkSheet *sheet, gboolean autoscroll)
1725 g_return_if_fail (sheet != NULL);
1726 g_return_if_fail (GTK_IS_SHEET (sheet));
1728 sheet->autoscroll = autoscroll;
1732 gtk_sheet_autoscroll (GtkSheet *sheet)
1734 g_return_val_if_fail (sheet != NULL, FALSE);
1735 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
1737 return sheet->autoscroll;
1742 gtk_sheet_set_justify_entry (GtkSheet *sheet, gboolean justify)
1744 g_return_if_fail (sheet != NULL);
1745 g_return_if_fail (GTK_IS_SHEET (sheet));
1747 sheet->justify_entry = justify;
1751 gtk_sheet_justify_entry (GtkSheet *sheet)
1753 g_return_val_if_fail (sheet != NULL, FALSE);
1754 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
1756 return sheet->justify_entry;
1762 gtk_sheet_freeze (GtkSheet *sheet)
1764 g_return_if_fail (sheet != NULL);
1765 g_return_if_fail (GTK_IS_SHEET (sheet));
1767 sheet->freeze_count++;
1768 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IS_FROZEN);
1772 gtk_sheet_thaw (GtkSheet *sheet)
1774 g_return_if_fail (sheet != NULL);
1775 g_return_if_fail (GTK_IS_SHEET (sheet));
1777 if (sheet->freeze_count == 0) return;
1779 sheet->freeze_count--;
1780 if (sheet->freeze_count > 0) return;
1782 adjust_scrollbars (sheet);
1784 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IS_FROZEN);
1786 sheet->old_vadjustment = -1.;
1787 sheet->old_hadjustment = -1.;
1789 if (sheet->hadjustment)
1790 g_signal_emit_by_name (sheet->hadjustment,
1792 if (sheet->vadjustment)
1793 g_signal_emit_by_name (sheet->vadjustment,
1796 if (sheet->state == GTK_STATE_NORMAL)
1797 if (sheet->entry_widget && GTK_WIDGET_MAPPED (sheet->entry_widget))
1799 gtk_sheet_activate_cell (sheet, sheet->active_cell.row,
1800 sheet->active_cell.col);
1806 gtk_sheet_set_row_titles_width (GtkSheet *sheet, guint width)
1808 if (width < COLUMN_MIN_WIDTH) return;
1810 sheet->row_title_area.width = width;
1812 adjust_scrollbars (sheet);
1814 sheet->old_hadjustment = -1.;
1815 if (sheet->hadjustment)
1816 g_signal_emit_by_name (sheet->hadjustment,
1818 size_allocate_global_button (sheet);
1822 gtk_sheet_set_column_titles_height (GtkSheet *sheet, guint height)
1824 if (height < DEFAULT_ROW_HEIGHT (GTK_WIDGET (sheet))) return;
1826 sheet->column_title_area.height = height;
1828 adjust_scrollbars (sheet);
1830 sheet->old_vadjustment = -1.;
1831 if (sheet->vadjustment)
1832 g_signal_emit_by_name (sheet->vadjustment,
1834 size_allocate_global_button (sheet);
1838 gtk_sheet_show_column_titles (GtkSheet *sheet)
1842 if (sheet->column_titles_visible) return;
1844 sheet->column_titles_visible = TRUE;
1847 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1849 gdk_window_show (sheet->column_title_window);
1850 gdk_window_move_resize (sheet->column_title_window,
1851 sheet->column_title_area.x,
1852 sheet->column_title_area.y,
1853 sheet->column_title_area.width,
1854 sheet->column_title_area.height);
1856 for (col = MIN_VISIBLE_COLUMN (sheet);
1857 col <= MAX_VISIBLE_COLUMN (sheet);
1860 GtkSheetButton *button = xxx_column_button (sheet, col);
1861 GtkSheetChild *child = button->child;
1863 gtk_sheet_child_show (child);
1864 gtk_sheet_button_free (button);
1866 adjust_scrollbars (sheet);
1869 sheet->old_vadjustment = -1.;
1870 if (sheet->vadjustment)
1871 g_signal_emit_by_name (sheet->vadjustment,
1873 size_allocate_global_button (sheet);
1878 gtk_sheet_show_row_titles (GtkSheet *sheet)
1882 if (sheet->row_titles_visible) return;
1884 sheet->row_titles_visible = TRUE;
1887 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1889 gdk_window_show (sheet->row_title_window);
1890 gdk_window_move_resize (sheet->row_title_window,
1891 sheet->row_title_area.x,
1892 sheet->row_title_area.y,
1893 sheet->row_title_area.width,
1894 sheet->row_title_area.height);
1896 for (row = MIN_VISIBLE_ROW (sheet);
1897 row <= MAX_VISIBLE_ROW (sheet);
1900 const GtkSheetButton *button = yyy_row_button (sheet, row);
1901 GtkSheetChild *child = button->child;
1905 gtk_sheet_child_show (child);
1908 adjust_scrollbars (sheet);
1911 sheet->old_hadjustment = -1.;
1912 if (sheet->hadjustment)
1913 g_signal_emit_by_name (sheet->hadjustment,
1915 size_allocate_global_button (sheet);
1919 gtk_sheet_hide_column_titles (GtkSheet *sheet)
1923 if (!sheet->column_titles_visible) return;
1925 sheet->column_titles_visible = FALSE;
1927 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1929 if (sheet->column_title_window)
1930 gdk_window_hide (sheet->column_title_window);
1931 if (GTK_WIDGET_VISIBLE (sheet->button))
1932 gtk_widget_hide (sheet->button);
1934 for (col = MIN_VISIBLE_COLUMN (sheet);
1935 col <= MAX_VISIBLE_COLUMN (sheet);
1938 GtkSheetButton *button = xxx_column_button (sheet, col);
1939 GtkSheetChild *child = button->child;
1941 gtk_sheet_child_hide (child);
1942 gtk_sheet_button_free (button);
1944 adjust_scrollbars (sheet);
1947 sheet->old_vadjustment = -1.;
1948 if (sheet->vadjustment)
1949 g_signal_emit_by_name (sheet->vadjustment,
1954 gtk_sheet_hide_row_titles (GtkSheet *sheet)
1958 if (!sheet->row_titles_visible) return;
1960 sheet->row_titles_visible = FALSE;
1963 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1965 if (sheet->row_title_window)
1966 gdk_window_hide (sheet->row_title_window);
1967 if (GTK_WIDGET_VISIBLE (sheet->button))
1968 gtk_widget_hide (sheet->button);
1969 for (row = MIN_VISIBLE_ROW (sheet);
1970 row <= MAX_VISIBLE_ROW (sheet);
1973 const GtkSheetButton *button = yyy_row_button (sheet, row);
1974 GtkSheetChild *child = button->child;
1977 gtk_sheet_child_hide (child);
1979 adjust_scrollbars (sheet);
1982 sheet->old_hadjustment = -1.;
1983 if (sheet->hadjustment)
1984 g_signal_emit_by_name (sheet->hadjustment,
1989 gtk_sheet_column_titles_visible (GtkSheet *sheet)
1991 g_return_val_if_fail (sheet != NULL, FALSE);
1992 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
1993 return sheet->column_titles_visible;
1997 gtk_sheet_row_titles_visible (GtkSheet *sheet)
1999 g_return_val_if_fail (sheet != NULL, FALSE);
2000 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
2001 return sheet->row_titles_visible;
2005 gtk_sheet_moveto (GtkSheet *sheet,
2012 guint width, height;
2014 gint min_row, min_col;
2016 g_return_if_fail (sheet != NULL);
2017 g_return_if_fail (GTK_IS_SHEET (sheet));
2018 g_return_if_fail (sheet->hadjustment != NULL);
2019 g_return_if_fail (sheet->vadjustment != NULL);
2021 if (row < 0 || row >= yyy_row_count (sheet))
2023 if (column < 0 || column >= xxx_column_count (sheet))
2026 height = sheet->sheet_window_height;
2027 width = sheet->sheet_window_width;
2029 /* adjust vertical scrollbar */
2030 if (row >= 0 && row_align >= 0.0)
2032 y = ROW_TOP_YPIXEL (sheet, row) - sheet->voffset
2033 - (gint) ( row_align * height + (1.0 - row_align)
2034 * yyy_row_height (sheet, row));
2036 /* This forces the sheet to scroll when you don't see the entire cell */
2039 if (row_align >= 1.0)
2041 while (min_row >= 0 && min_row > MIN_VISIBLE_ROW (sheet))
2043 if (yyy_row_is_visible (sheet, min_row))
2044 adjust += yyy_row_height (sheet, min_row);
2046 if (adjust >= height)
2052 min_row = MAX (min_row, 0);
2056 y = ROW_TOP_YPIXEL (sheet, min_row) - sheet->voffset +
2057 yyy_row_height (sheet, min_row) - 1;
2061 sheet->vadjustment->value = 0.0;
2063 sheet->vadjustment->value = y;
2065 sheet->old_vadjustment = -1.;
2066 g_signal_emit_by_name (sheet->vadjustment,
2071 /* adjust horizontal scrollbar */
2072 if (column >= 0 && col_align >= 0.0)
2074 x = COLUMN_LEFT_XPIXEL (sheet, column) - sheet->hoffset
2075 - (gint) ( col_align*width + (1.0 - col_align)*
2076 xxx_column_width (sheet, column));
2078 /* This forces the sheet to scroll when you don't see the entire cell */
2081 if (col_align == 1.0)
2083 while (min_col >= 0 && min_col > MIN_VISIBLE_COLUMN (sheet))
2085 if (xxx_column_is_visible (sheet, min_col))
2086 adjust += xxx_column_width (sheet, min_col);
2088 if (adjust >= width)
2094 min_col = MAX (min_col, 0);
2095 x = COLUMN_LEFT_XPIXEL (sheet, min_col) - sheet->hoffset +
2096 xxx_column_width (sheet, min_col) - 1;
2100 sheet->hadjustment->value = 0.0;
2102 sheet->hadjustment->value = x;
2104 sheet->old_vadjustment = -1.;
2105 g_signal_emit_by_name (sheet->hadjustment,
2112 gtk_sheet_columns_set_resizable (GtkSheet *sheet, gboolean resizable)
2114 g_return_if_fail (sheet != NULL);
2115 g_return_if_fail (GTK_IS_SHEET (sheet));
2117 sheet->columns_resizable = resizable;
2121 gtk_sheet_columns_resizable (GtkSheet *sheet)
2123 g_return_val_if_fail (sheet != NULL, FALSE);
2124 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
2126 return sheet->columns_resizable;
2131 gtk_sheet_rows_set_resizable (GtkSheet *sheet, gboolean resizable)
2133 g_return_if_fail (sheet != NULL);
2134 g_return_if_fail (GTK_IS_SHEET (sheet));
2136 sheet->rows_resizable = resizable;
2140 gtk_sheet_rows_resizable (GtkSheet *sheet)
2142 g_return_val_if_fail (sheet != NULL, FALSE);
2143 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
2145 return sheet->rows_resizable;
2150 gtk_sheet_select_row (GtkSheet * sheet,
2153 g_return_if_fail (sheet != NULL);
2154 g_return_if_fail (GTK_IS_SHEET (sheet));
2156 if (row < 0 || row >= yyy_row_count (sheet))
2159 if (sheet->state != GTK_SHEET_NORMAL)
2160 gtk_sheet_real_unselect_range (sheet, NULL);
2162 gtk_sheet_deactivate_cell (sheet);
2164 sheet->state = GTK_SHEET_ROW_SELECTED;
2165 sheet->range.row0 = row;
2166 sheet->range.col0 = 0;
2167 sheet->range.rowi = row;
2168 sheet->range.coli = xxx_column_count (sheet) - 1;
2169 sheet->active_cell.row = row;
2170 sheet->active_cell.col = 0;
2172 g_signal_emit (sheet, sheet_signals[SELECT_ROW], 0, row);
2173 gtk_sheet_real_select_range (sheet, NULL);
2178 gtk_sheet_select_column (GtkSheet * sheet, gint column)
2180 g_return_if_fail (sheet != NULL);
2181 g_return_if_fail (GTK_IS_SHEET (sheet));
2183 if (column < 0 || column >= xxx_column_count (sheet))
2186 if (sheet->state != GTK_SHEET_NORMAL)
2187 gtk_sheet_real_unselect_range (sheet, NULL);
2189 gtk_sheet_deactivate_cell (sheet);
2192 sheet->state = GTK_SHEET_COLUMN_SELECTED;
2193 sheet->range.row0 = 0;
2194 sheet->range.col0 = column;
2195 sheet->range.rowi = yyy_row_count (sheet) - 1;
2196 sheet->range.coli = column;
2197 sheet->active_cell.row = 0;
2198 sheet->active_cell.col = column;
2200 g_signal_emit (sheet, sheet_signals[SELECT_COLUMN], 0, column);
2201 gtk_sheet_real_select_range (sheet, NULL);
2208 gtk_sheet_range_isvisible (const GtkSheet * sheet,
2209 GtkSheetRange range)
2211 g_return_val_if_fail (sheet != NULL, FALSE);
2213 if (range.row0 < 0 || range.row0 >= yyy_row_count (sheet))
2216 if (range.rowi < 0 || range.rowi >= yyy_row_count (sheet))
2219 if (range.col0 < 0 || range.col0 >= xxx_column_count (sheet))
2222 if (range.coli < 0 || range.coli >= xxx_column_count (sheet))
2225 if (range.rowi < MIN_VISIBLE_ROW (sheet))
2228 if (range.row0 > MAX_VISIBLE_ROW (sheet))
2231 if (range.coli < MIN_VISIBLE_COLUMN (sheet))
2234 if (range.col0 > MAX_VISIBLE_COLUMN (sheet))
2241 gtk_sheet_cell_isvisible (GtkSheet * sheet,
2242 gint row, gint column)
2244 GtkSheetRange range;
2247 range.col0 = column;
2249 range.coli = column;
2251 return gtk_sheet_range_isvisible (sheet, range);
2255 gtk_sheet_get_visible_range (GtkSheet *sheet, GtkSheetRange *range)
2257 g_return_if_fail (sheet != NULL);
2258 g_return_if_fail (GTK_IS_SHEET (sheet)) ;
2259 g_return_if_fail (range != NULL);
2261 range->row0 = MIN_VISIBLE_ROW (sheet);
2262 range->col0 = MIN_VISIBLE_COLUMN (sheet);
2263 range->rowi = MAX_VISIBLE_ROW (sheet);
2264 range->coli = MAX_VISIBLE_COLUMN (sheet);
2268 gtk_sheet_get_vadjustment (GtkSheet * sheet)
2270 g_return_val_if_fail (sheet != NULL, NULL);
2271 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
2273 return sheet->vadjustment;
2277 gtk_sheet_get_hadjustment (GtkSheet * sheet)
2279 g_return_val_if_fail (sheet != NULL, NULL);
2280 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
2282 return sheet->hadjustment;
2286 gtk_sheet_set_vadjustment (GtkSheet *sheet,
2287 GtkAdjustment *adjustment)
2289 GtkAdjustment *old_adjustment;
2291 g_return_if_fail (sheet != NULL);
2292 g_return_if_fail (GTK_IS_SHEET (sheet));
2294 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2296 if (sheet->vadjustment == adjustment)
2299 old_adjustment = sheet->vadjustment;
2301 if (sheet->vadjustment)
2303 g_signal_handlers_disconnect_matched (sheet->vadjustment,
2304 G_SIGNAL_MATCH_DATA,
2307 g_object_unref (sheet->vadjustment);
2310 sheet->vadjustment = adjustment;
2312 if (sheet->vadjustment)
2314 g_object_ref (sheet->vadjustment);
2315 g_object_ref_sink (sheet->vadjustment);
2317 g_signal_connect (sheet->vadjustment, "value_changed",
2318 G_CALLBACK (vadjustment_value_changed),
2322 if (!sheet->vadjustment || !old_adjustment)
2324 gtk_widget_queue_resize (GTK_WIDGET (sheet));
2328 sheet->old_vadjustment = sheet->vadjustment->value;
2332 gtk_sheet_set_hadjustment (GtkSheet *sheet,
2333 GtkAdjustment *adjustment)
2335 GtkAdjustment *old_adjustment;
2337 g_return_if_fail (sheet != NULL);
2338 g_return_if_fail (GTK_IS_SHEET (sheet));
2340 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2342 if (sheet->hadjustment == adjustment)
2345 old_adjustment = sheet->hadjustment;
2347 if (sheet->hadjustment)
2349 g_signal_handlers_disconnect_matched (sheet->hadjustment,
2350 G_SIGNAL_MATCH_DATA,
2353 g_object_unref (sheet->hadjustment);
2356 sheet->hadjustment = adjustment;
2358 if (sheet->hadjustment)
2360 g_object_ref (sheet->hadjustment);
2361 g_object_ref_sink (sheet->hadjustment);
2363 g_signal_connect (sheet->hadjustment, "value_changed",
2364 G_CALLBACK (hadjustment_value_changed),
2368 if (!sheet->hadjustment || !old_adjustment)
2370 gtk_widget_queue_resize (GTK_WIDGET (sheet));
2374 sheet->old_hadjustment = sheet->hadjustment->value;
2378 gtk_sheet_set_scroll_adjustments (GtkSheet *sheet,
2379 GtkAdjustment *hadjustment,
2380 GtkAdjustment *vadjustment)
2382 if (sheet->hadjustment != hadjustment)
2383 gtk_sheet_set_hadjustment (sheet, hadjustment);
2385 if (sheet->vadjustment != vadjustment)
2386 gtk_sheet_set_vadjustment (sheet, vadjustment);
2390 gtk_sheet_finalize (GObject * object)
2394 g_return_if_fail (object != NULL);
2395 g_return_if_fail (GTK_IS_SHEET (object));
2397 sheet = GTK_SHEET (object);
2399 if (G_OBJECT_CLASS (parent_class)->finalize)
2400 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
2404 gtk_sheet_dispose (GObject * object)
2406 GtkSheet *sheet = GTK_SHEET (object);
2409 g_return_if_fail (object != NULL);
2410 g_return_if_fail (GTK_IS_SHEET (object));
2412 if ( sheet->dispose_has_run )
2415 if (sheet->model) g_object_unref (sheet->model);
2416 sheet->dispose_has_run = TRUE;
2418 g_object_unref (sheet->entry_container);
2419 g_object_unref (sheet->button);
2421 /* unref adjustments */
2422 if (sheet->hadjustment)
2424 g_signal_handlers_disconnect_matched (sheet->hadjustment,
2425 G_SIGNAL_MATCH_DATA,
2429 g_object_unref (sheet->hadjustment);
2430 sheet->hadjustment = NULL;
2433 if (sheet->vadjustment)
2435 g_signal_handlers_disconnect_matched (sheet->vadjustment,
2436 G_SIGNAL_MATCH_DATA,
2440 g_object_unref (sheet->vadjustment);
2442 sheet->vadjustment = NULL;
2445 children = sheet->children;
2448 GtkSheetChild *child = (GtkSheetChild *)children->data;
2449 if (child && child->widget)
2450 gtk_sheet_remove (GTK_CONTAINER (sheet), child->widget);
2451 children = sheet->children;
2453 sheet->children = NULL;
2455 if (G_OBJECT_CLASS (parent_class)->dispose)
2456 (*G_OBJECT_CLASS (parent_class)->dispose) (object);
2460 gtk_sheet_style_set (GtkWidget *widget,
2461 GtkStyle *previous_style)
2465 g_return_if_fail (widget != NULL);
2466 g_return_if_fail (GTK_IS_SHEET (widget));
2468 if (GTK_WIDGET_CLASS (parent_class)->style_set)
2469 (*GTK_WIDGET_CLASS (parent_class)->style_set) (widget, previous_style);
2471 sheet = GTK_SHEET (widget);
2473 if (GTK_WIDGET_REALIZED (widget))
2475 gtk_style_set_background (widget->style, widget->window, widget->state);
2481 gtk_sheet_realize (GtkWidget * widget)
2484 GdkWindowAttr attributes;
2485 gint attributes_mask;
2486 GdkGCValues values, auxvalues;
2487 GdkColormap *colormap;
2488 GtkSheetChild *child;
2491 g_return_if_fail (widget != NULL);
2492 g_return_if_fail (GTK_IS_SHEET (widget));
2494 sheet = GTK_SHEET (widget);
2496 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2498 attributes.window_type = GDK_WINDOW_CHILD;
2499 attributes.x = widget->allocation.x;
2500 attributes.y = widget->allocation.y;
2501 attributes.width = widget->allocation.width;
2502 attributes.height = widget->allocation.height;
2503 attributes.wclass = GDK_INPUT_OUTPUT;
2505 attributes.visual = gtk_widget_get_visual (widget);
2506 attributes.colormap = gtk_widget_get_colormap (widget);
2508 attributes.event_mask = gtk_widget_get_events (widget);
2509 attributes.event_mask |= (GDK_EXPOSURE_MASK |
2510 GDK_BUTTON_PRESS_MASK |
2511 GDK_BUTTON_RELEASE_MASK |
2512 GDK_KEY_PRESS_MASK |
2513 GDK_POINTER_MOTION_MASK |
2514 GDK_POINTER_MOTION_HINT_MASK);
2515 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP |
2518 attributes.cursor = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
2521 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2523 gdk_window_set_user_data (widget->window, sheet);
2525 widget->style = gtk_style_attach (widget->style, widget->window);
2527 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2530 if (sheet->row_titles_visible)
2531 attributes.x = sheet->row_title_area.width;
2533 attributes.width = sheet->column_title_area.width;
2534 attributes.height = sheet->column_title_area.height;
2536 /* column - title window */
2537 sheet->column_title_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2538 gdk_window_set_user_data (sheet->column_title_window, sheet);
2539 gtk_style_set_background (widget->style, sheet->column_title_window, GTK_STATE_NORMAL);
2543 if (sheet->column_titles_visible)
2544 attributes.y = sheet->column_title_area.height;
2545 attributes.width = sheet->row_title_area.width;
2546 attributes.height = sheet->row_title_area.height;
2548 /* row - title window */
2549 sheet->row_title_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2550 gdk_window_set_user_data (sheet->row_title_window, sheet);
2551 gtk_style_set_background (widget->style, sheet->row_title_window, GTK_STATE_NORMAL);
2553 /* sheet - window */
2554 attributes.cursor = gdk_cursor_new (GDK_PLUS);
2558 attributes.width = sheet->sheet_window_width,
2559 attributes.height = sheet->sheet_window_height;
2561 sheet->sheet_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2562 gdk_window_set_user_data (sheet->sheet_window, sheet);
2564 gdk_cursor_unref (attributes.cursor);
2566 gdk_window_set_background (sheet->sheet_window, &widget->style->white);
2567 gdk_window_show (sheet->sheet_window);
2569 /* backing_pixmap */
2570 gtk_sheet_make_backing_pixmap (sheet, 0, 0);
2574 gdk_gc_unref (sheet->fg_gc);
2576 gdk_gc_unref (sheet->bg_gc);
2577 sheet->fg_gc = gdk_gc_new (widget->window);
2578 sheet->bg_gc = gdk_gc_new (widget->window);
2580 colormap = gtk_widget_get_colormap (widget);
2582 gdk_color_white (colormap, &widget->style->white);
2583 gdk_color_black (colormap, &widget->style->black);
2585 gdk_gc_get_values (sheet->fg_gc, &auxvalues);
2587 values.foreground = widget->style->white;
2588 values.function = GDK_INVERT;
2589 values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2591 gdk_gc_unref (sheet->xor_gc);
2592 sheet->xor_gc = gdk_gc_new_with_values (widget->window,
2598 gtk_widget_set_parent_window (sheet->entry_widget, sheet->sheet_window);
2599 gtk_widget_set_parent (sheet->entry_widget, GTK_WIDGET (sheet));
2601 gtk_widget_set_parent_window (sheet->button, sheet->sheet_window);
2602 gtk_widget_set_parent (sheet->button, GTK_WIDGET (sheet));
2604 if (!sheet->cursor_drag)
2605 sheet->cursor_drag = gdk_cursor_new (GDK_PLUS);
2607 if (sheet->column_titles_visible)
2608 gdk_window_show (sheet->column_title_window);
2609 if (sheet->row_titles_visible)
2610 gdk_window_show (sheet->row_title_window);
2612 size_allocate_row_title_buttons (sheet);
2613 size_allocate_column_title_buttons (sheet);
2615 children = sheet->children;
2618 child = children->data;
2619 children = children->next;
2621 gtk_sheet_realize_child (sheet, child);
2624 gtk_sheet_update_primary_selection (sheet);
2628 create_global_button (GtkSheet *sheet)
2630 sheet->button = gtk_button_new_with_label (" ");
2632 g_object_ref_sink (sheet->button);
2634 g_signal_connect (sheet->button,
2636 G_CALLBACK (global_button_clicked),
2641 size_allocate_global_button (GtkSheet *sheet)
2643 GtkAllocation allocation;
2645 if (!sheet->column_titles_visible) return;
2646 if (!sheet->row_titles_visible) return;
2648 gtk_widget_size_request (sheet->button, NULL);
2652 allocation.width = sheet->row_title_area.width;
2653 allocation.height = sheet->column_title_area.height;
2655 gtk_widget_size_allocate (sheet->button, &allocation);
2656 gtk_widget_show (sheet->button);
2660 global_button_clicked (GtkWidget *widget, gpointer data)
2664 gtk_sheet_click_cell (GTK_SHEET (data), - 1, - 1, &veto);
2665 gtk_widget_grab_focus (GTK_WIDGET (data));
2670 gtk_sheet_unrealize (GtkWidget * widget)
2674 g_return_if_fail (widget != NULL);
2675 g_return_if_fail (GTK_IS_SHEET (widget));
2677 sheet = GTK_SHEET (widget);
2679 gdk_cursor_destroy (sheet->cursor_drag);
2681 gdk_gc_destroy (sheet->xor_gc);
2682 gdk_gc_destroy (sheet->fg_gc);
2683 gdk_gc_destroy (sheet->bg_gc);
2685 gdk_window_destroy (sheet->sheet_window);
2686 gdk_window_destroy (sheet->column_title_window);
2687 gdk_window_destroy (sheet->row_title_window);
2691 g_object_unref (sheet->pixmap);
2692 sheet->pixmap = NULL;
2695 sheet->column_title_window = NULL;
2696 sheet->sheet_window = NULL;
2697 sheet->cursor_drag = NULL;
2698 sheet->xor_gc = NULL;
2699 sheet->fg_gc = NULL;
2700 sheet->bg_gc = NULL;
2702 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
2703 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2707 gtk_sheet_map (GtkWidget * widget)
2709 GtkSheet *sheet = GTK_SHEET (widget);
2710 GtkSheetChild *child;
2713 g_return_if_fail (widget != NULL);
2714 g_return_if_fail (GTK_IS_SHEET (widget));
2716 if (!GTK_WIDGET_MAPPED (widget))
2718 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2720 if (!sheet->cursor_drag) sheet->cursor_drag = gdk_cursor_new (GDK_PLUS);
2722 gdk_window_show (widget->window);
2724 gdk_window_show (sheet->sheet_window);
2726 if (sheet->column_titles_visible)
2728 size_allocate_column_title_buttons (sheet);
2729 gdk_window_show (sheet->column_title_window);
2731 if (sheet->row_titles_visible)
2733 size_allocate_row_title_buttons (sheet);
2734 gdk_window_show (sheet->row_title_window);
2737 if (!GTK_WIDGET_MAPPED (sheet->entry_widget)
2738 && sheet->active_cell.row >= 0
2739 && sheet->active_cell.col >= 0 )
2741 gtk_widget_show (sheet->entry_widget);
2742 gtk_widget_map (sheet->entry_widget);
2745 if (GTK_WIDGET_VISIBLE (sheet->button) &&
2746 !GTK_WIDGET_MAPPED (sheet->button))
2748 gtk_widget_show (sheet->button);
2749 gtk_widget_map (sheet->button);
2752 if (GTK_BIN (sheet->button)->child)
2753 if (GTK_WIDGET_VISIBLE (GTK_BIN (sheet->button)->child) &&
2754 !GTK_WIDGET_MAPPED (GTK_BIN (sheet->button)->child))
2755 gtk_widget_map (GTK_BIN (sheet->button)->child);
2757 gtk_sheet_range_draw (sheet, NULL);
2758 gtk_sheet_activate_cell (sheet,
2759 sheet->active_cell.row,
2760 sheet->active_cell.col);
2762 children = sheet->children;
2765 child = children->data;
2766 children = children->next;
2768 if (GTK_WIDGET_VISIBLE (child->widget) &&
2769 !GTK_WIDGET_MAPPED (child->widget))
2771 gtk_widget_map (child->widget);
2772 gtk_sheet_position_child (sheet, child);
2780 gtk_sheet_unmap (GtkWidget * widget)
2783 GtkSheetChild *child;
2786 g_return_if_fail (widget != NULL);
2787 g_return_if_fail (GTK_IS_SHEET (widget));
2789 sheet = GTK_SHEET (widget);
2791 if (GTK_WIDGET_MAPPED (widget))
2793 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2795 gdk_window_hide (sheet->sheet_window);
2796 if (sheet->column_titles_visible)
2797 gdk_window_hide (sheet->column_title_window);
2798 if (sheet->row_titles_visible)
2799 gdk_window_hide (sheet->row_title_window);
2800 gdk_window_hide (widget->window);
2802 if (GTK_WIDGET_MAPPED (sheet->entry_widget))
2803 gtk_widget_unmap (sheet->entry_widget);
2805 if (GTK_WIDGET_MAPPED (sheet->button))
2806 gtk_widget_unmap (sheet->button);
2808 children = sheet->children;
2811 child = children->data;
2812 children = children->next;
2814 if (GTK_WIDGET_VISIBLE (child->widget) &&
2815 GTK_WIDGET_MAPPED (child->widget))
2817 gtk_widget_unmap (child->widget);
2826 gtk_sheet_cell_draw_default (GtkSheet *sheet, gint row, gint col)
2829 GdkGC *fg_gc, *bg_gc;
2830 GtkSheetCellAttr attributes;
2833 g_return_if_fail (sheet != NULL);
2835 /* bail now if we arn't drawable yet */
2836 if (!GTK_WIDGET_DRAWABLE (sheet)) return;
2838 if (row < 0 || row >= yyy_row_count (sheet)) return;
2839 if (col < 0 || col >= xxx_column_count (sheet)) return;
2840 if (! xxx_column_is_visible (sheet, col)) return;
2841 if (! yyy_row_is_visible (sheet, row)) return;
2843 widget = GTK_WIDGET (sheet);
2845 gtk_sheet_get_attributes (sheet, row, col, &attributes);
2847 /* select GC for background rectangle */
2848 gdk_gc_set_foreground (sheet->fg_gc, &attributes.foreground);
2849 gdk_gc_set_foreground (sheet->bg_gc, &attributes.background);
2851 fg_gc = sheet->fg_gc;
2852 bg_gc = sheet->bg_gc;
2854 area.x = COLUMN_LEFT_XPIXEL (sheet, col);
2855 area.y = ROW_TOP_YPIXEL (sheet, row);
2856 area.width= xxx_column_width (sheet, col);
2857 area.height = yyy_row_height (sheet, row);
2859 gdk_draw_rectangle (sheet->pixmap,
2867 gdk_gc_set_line_attributes (sheet->fg_gc, 1, 0, 0, 0);
2869 if (sheet->show_grid)
2871 gdk_gc_set_foreground (sheet->bg_gc, &sheet->grid_color);
2873 gdk_draw_rectangle (sheet->pixmap,
2877 area.width, area.height);
2882 gtk_sheet_cell_draw_label (GtkSheet *sheet, gint row, gint col)
2887 gint text_width, text_height, y;
2889 gint size, sizel, sizer;
2890 GdkGC *fg_gc, *bg_gc;
2891 GtkSheetCellAttr attributes;
2892 PangoLayout *layout;
2893 PangoRectangle rect;
2894 PangoRectangle logical_rect;
2895 PangoLayoutLine *line;
2896 PangoFontMetrics *metrics;
2897 PangoContext *context = gtk_widget_get_pango_context (GTK_WIDGET (sheet));
2898 gint ascent, descent, y_pos;
2902 g_return_if_fail (sheet != NULL);
2904 /* bail now if we aren't drawable yet */
2905 if (!GTK_WIDGET_DRAWABLE (sheet))
2908 label = gtk_sheet_cell_get_text (sheet, row, col);
2912 if (row < 0 || row >= yyy_row_count (sheet)) return;
2913 if (col < 0 || col >= xxx_column_count (sheet)) return;
2914 if (! xxx_column_is_visible (sheet, col)) return;
2915 if (!yyy_row_is_visible (sheet, row)) return;
2918 widget = GTK_WIDGET (sheet);
2920 gtk_sheet_get_attributes (sheet, row, col, &attributes);
2922 /* select GC for background rectangle */
2923 gdk_gc_set_foreground (sheet->fg_gc, &attributes.foreground);
2924 gdk_gc_set_foreground (sheet->bg_gc, &attributes.background);
2926 fg_gc = sheet->fg_gc;
2927 bg_gc = sheet->bg_gc;
2929 area.x = COLUMN_LEFT_XPIXEL (sheet, col);
2930 area.y = ROW_TOP_YPIXEL (sheet, row);
2931 area.width = xxx_column_width (sheet, col);
2932 area.height = yyy_row_height (sheet, row);
2935 layout = gtk_widget_create_pango_layout (GTK_WIDGET (sheet), label);
2936 dispose_string (sheet, label);
2937 pango_layout_set_font_description (layout, attributes.font_desc);
2939 pango_layout_get_pixel_extents (layout, NULL, &rect);
2941 line = pango_layout_get_lines (layout)->data;
2942 pango_layout_line_get_extents (line, NULL, &logical_rect);
2944 metrics = pango_context_get_metrics (context,
2945 attributes.font_desc,
2946 pango_context_get_language (context));
2948 ascent = pango_font_metrics_get_ascent (metrics) / PANGO_SCALE;
2949 descent = pango_font_metrics_get_descent (metrics) / PANGO_SCALE;
2951 pango_font_metrics_unref (metrics);
2953 /* Align primarily for locale's ascent / descent */
2955 logical_rect.height /= PANGO_SCALE;
2956 logical_rect.y /= PANGO_SCALE;
2957 y_pos = area.height - logical_rect.height;
2959 if (logical_rect.height > area.height)
2960 y_pos = (logical_rect.height - area.height - 2 * CELLOFFSET) / 2;
2963 else if (y_pos + logical_rect.height > area.height)
2964 y_pos = area.height - logical_rect.height;
2966 text_width = rect.width;
2967 text_height = rect.height;
2968 y = area.y + y_pos - CELLOFFSET;
2970 switch (attributes.justification)
2972 case GTK_JUSTIFY_RIGHT:
2974 area.x +=area.width;
2976 for (i = col - 1; i >= MIN_VISIBLE_COLUMN (sheet); i--)
2978 if ( !gtk_sheet_cell_empty (sheet, row, i)) break;
2979 if (size >= text_width + CELLOFFSET) break;
2980 size +=xxx_column_width (sheet, i);
2981 xxx_column_set_right_column (sheet, i,
2983 xxx_column_right_column (sheet, i)));
2988 xoffset += area.width - text_width - 2 * CELLOFFSET -
2989 attributes.border.width / 2;
2991 case GTK_JUSTIFY_CENTER:
2992 sizel = area.width / 2;
2993 sizer = area.width / 2;
2994 area.x += area.width / 2;
2996 for (i = col + 1; i <= MAX_VISIBLE_COLUMN (sheet); i++)
2998 if ( ! gtk_sheet_cell_empty (sheet, row, i)) break;
2999 if (sizer >= text_width / 2) break;
3000 sizer += xxx_column_width (sheet, i);
3001 xxx_column_set_left_column (sheet, i,
3004 xxx_column_left_column (sheet, i)));
3006 for (i = col - 1; i >= MIN_VISIBLE_COLUMN (sheet); i--)
3008 if ( ! gtk_sheet_cell_empty (sheet, row, i)) break;
3009 if (sizel >= text_width / 2) break;
3010 sizel +=xxx_column_width (sheet, i);
3011 xxx_column_set_right_column (sheet, i,
3013 xxx_column_right_column (sheet, i)));
3015 size = MIN (sizel, sizer);
3018 xoffset += sizel - text_width / 2 - CELLOFFSET;
3019 area.width = sizel + sizer;
3021 case GTK_JUSTIFY_LEFT:
3025 for (i = col + 1; i <= MAX_VISIBLE_COLUMN (sheet); i++)
3027 if (! gtk_sheet_cell_empty (sheet, row, i)) break;
3028 if (size >= text_width + CELLOFFSET) break;
3029 size +=xxx_column_width (sheet, i);
3030 xxx_column_set_left_column (sheet, i,
3033 xxx_column_left_column (sheet, i)));
3038 xoffset += attributes.border.width / 2;
3042 gdk_gc_set_clip_rectangle (fg_gc, &area);
3045 gdk_draw_layout (sheet->pixmap, fg_gc,
3046 area.x + xoffset + CELLOFFSET,
3050 gdk_gc_set_clip_rectangle (fg_gc, NULL);
3051 g_object_unref (layout);
3053 gdk_draw_pixmap (sheet->sheet_window,
3054 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
3066 gtk_sheet_range_draw (GtkSheet *sheet, const GtkSheetRange *range)
3069 GtkSheetRange drawing_range;
3072 g_return_if_fail (sheet != NULL);
3073 g_return_if_fail (GTK_SHEET (sheet));
3075 if (!GTK_WIDGET_DRAWABLE (GTK_WIDGET (sheet))) return;
3076 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
3077 if (!GTK_WIDGET_MAPPED (GTK_WIDGET (sheet))) return;
3081 drawing_range.row0 = MIN_VISIBLE_ROW (sheet);
3082 drawing_range.col0 = MIN_VISIBLE_COLUMN (sheet);
3083 drawing_range.rowi = MIN (MAX_VISIBLE_ROW (sheet),
3084 yyy_row_count (sheet) - 1);
3085 drawing_range.coli = MAX_VISIBLE_COLUMN (sheet);
3088 gdk_draw_rectangle (sheet->pixmap,
3089 GTK_WIDGET (sheet)->style->white_gc,
3092 sheet->sheet_window_width,
3093 sheet->sheet_window_height);
3097 drawing_range.row0 = MAX (range->row0, MIN_VISIBLE_ROW (sheet));
3098 drawing_range.col0 = MAX (range->col0, MIN_VISIBLE_COLUMN (sheet));
3099 drawing_range.rowi = MIN (range->rowi, MAX_VISIBLE_ROW (sheet));
3100 drawing_range.coli = MIN (range->coli, MAX_VISIBLE_COLUMN (sheet));
3103 if (drawing_range.coli == xxx_column_count (sheet) - 1)
3105 area.x = COLUMN_LEFT_XPIXEL (sheet,
3106 xxx_column_count (sheet) - 1) +
3107 xxx_column_width (sheet, xxx_column_count (sheet) - 1) + 1;
3111 gdk_gc_set_foreground (sheet->fg_gc, &sheet->bg_color);
3113 gdk_draw_rectangle (sheet->pixmap,
3117 sheet->sheet_window_width - area.x,
3118 sheet->sheet_window_height);
3120 gdk_draw_pixmap (sheet->sheet_window,
3121 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
3127 sheet->sheet_window_width - area.x,
3128 sheet->sheet_window_height);
3131 if (drawing_range.rowi == yyy_row_count (sheet) - 1)
3134 area.y = ROW_TOP_YPIXEL (sheet,
3135 yyy_row_count (sheet) - 1) +
3136 yyy_row_height (sheet, yyy_row_count (sheet) - 1) + 1;
3138 gdk_gc_set_foreground (sheet->fg_gc, &sheet->bg_color);
3140 gdk_draw_rectangle (sheet->pixmap,
3144 sheet->sheet_window_width,
3145 sheet->sheet_window_height - area.y);
3147 gdk_draw_pixmap (sheet->sheet_window,
3148 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
3154 sheet->sheet_window_width,
3155 sheet->sheet_window_height - area.y);
3158 for (i = drawing_range.row0; i <= drawing_range.rowi; i++)
3159 for (j = drawing_range.col0; j <= drawing_range.coli; j++)
3161 gtk_sheet_cell_draw_default (sheet, i, j);
3162 gtk_sheet_cell_draw_label (sheet, i, j);
3165 gtk_sheet_draw_backing_pixmap (sheet, drawing_range);
3167 if (sheet->state != GTK_SHEET_NORMAL &&
3168 gtk_sheet_range_isvisible (sheet, sheet->range))
3169 gtk_sheet_range_draw_selection (sheet, drawing_range);
3171 if (sheet->state == GTK_STATE_NORMAL &&
3172 sheet->active_cell.row >= drawing_range.row0 &&
3173 sheet->active_cell.row <= drawing_range.rowi &&
3174 sheet->active_cell.col >= drawing_range.col0 &&
3175 sheet->active_cell.col <= drawing_range.coli)
3176 gtk_sheet_show_active_cell (sheet);
3180 gtk_sheet_range_draw_selection (GtkSheet *sheet, GtkSheetRange range)
3186 if (range.col0 > sheet->range.coli || range.coli < sheet->range.col0 ||
3187 range.row0 > sheet->range.rowi || range.rowi < sheet->range.row0)
3190 if (!gtk_sheet_range_isvisible (sheet, range)) return;
3191 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
3195 range.col0 = MAX (sheet->range.col0, range.col0);
3196 range.coli = MIN (sheet->range.coli, range.coli);
3197 range.row0 = MAX (sheet->range.row0, range.row0);
3198 range.rowi = MIN (sheet->range.rowi, range.rowi);
3200 range.col0 = MAX (range.col0, MIN_VISIBLE_COLUMN (sheet));
3201 range.coli = MIN (range.coli, MAX_VISIBLE_COLUMN (sheet));
3202 range.row0 = MAX (range.row0, MIN_VISIBLE_ROW (sheet));
3203 range.rowi = MIN (range.rowi, MAX_VISIBLE_ROW (sheet));
3205 for (i = range.row0; i <= range.rowi; i++)
3207 for (j = range.col0; j <= range.coli; j++)
3210 if (gtk_sheet_cell_get_state (sheet, i, j) == GTK_STATE_SELECTED &&
3211 xxx_column_is_visible (sheet, j) && yyy_row_is_visible (sheet, i))
3214 area.x = COLUMN_LEFT_XPIXEL (sheet, j);
3215 area.y = ROW_TOP_YPIXEL (sheet, i);
3216 area.width= xxx_column_width (sheet, j);
3217 area.height = yyy_row_height (sheet, i);
3219 if (i == sheet->range.row0)
3221 area.y = area.y + 2;
3222 area.height = area.height - 2;
3224 if (i == sheet->range.rowi) area.height = area.height - 3;
3225 if (j == sheet->range.col0)
3227 area.x = area.x + 2;
3228 area.width = area.width - 2;
3230 if (j == sheet->range.coli) area.width = area.width - 3;
3232 if (i != sheet->active_cell.row || j != sheet->active_cell.col)
3234 gdk_draw_rectangle (sheet->sheet_window,
3237 area.x + 1, area.y + 1,
3238 area.width, area.height);
3245 gtk_sheet_draw_border (sheet, sheet->range);
3249 gtk_sheet_draw_backing_pixmap (GtkSheet *sheet, GtkSheetRange range)
3251 gint x, y, width, height;
3253 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
3255 x = COLUMN_LEFT_XPIXEL (sheet, range.col0);
3256 y = ROW_TOP_YPIXEL (sheet, range.row0);
3257 width = COLUMN_LEFT_XPIXEL (sheet, range.coli) - x +
3258 xxx_column_width (sheet, range.coli);
3260 height = ROW_TOP_YPIXEL (sheet, range.rowi)- y + yyy_row_height (sheet, range.rowi);
3262 if (range.row0 == sheet->range.row0)
3265 height = height + 5;
3267 if (range.rowi == sheet->range.rowi) height = height + 5;
3268 if (range.col0 == sheet->range.col0)
3273 if (range.coli == sheet->range.coli) width = width + 5;
3275 width = MIN (width, sheet->sheet_window_width - x);
3276 height = MIN (height, sheet->sheet_window_height - y);
3283 x = (sheet->row_titles_visible)
3284 ? MAX (x, sheet->row_title_area.width) : MAX (x, 0);
3285 y = (sheet->column_titles_visible)
3286 ? MAX (y, sheet->column_title_area.height) : MAX (y, 0);
3288 if (range.coli == xxx_column_count (sheet) - 1)
3289 width = sheet->sheet_window_width - x;
3290 if (range.rowi == yyy_row_count (sheet) - 1)
3291 height = sheet->sheet_window_height - y;
3293 gdk_draw_pixmap (sheet->sheet_window,
3294 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
3306 gtk_sheet_set_cell_text (GtkSheet *sheet, gint row, gint col, const gchar *text)
3308 GtkSheetCellAttr attributes;
3310 g_return_if_fail (sheet != NULL);
3311 g_return_if_fail (GTK_IS_SHEET (sheet));
3312 if (col >= xxx_column_count (sheet) || row >= yyy_row_count (sheet)) return;
3313 if (col < 0 || row < 0) return;
3315 gtk_sheet_get_attributes (sheet, row, col, &attributes);
3316 gtk_sheet_set_cell (sheet, row, col, attributes.justification, text);
3320 safe_strcmp (const gchar *s1, const gchar *s2)
3322 if ( !s1 && !s2) return 0;
3323 if ( !s1) return - 1;
3324 if ( !s2) return +1;
3325 return strcmp (s1, s2);
3329 gtk_sheet_set_cell (GtkSheet *sheet, gint row, gint col,
3330 GtkJustification justification,
3333 GSheetModel *model ;
3337 GtkSheetRange range;
3339 GtkSheetCellAttr attributes;
3341 g_return_if_fail (sheet != NULL);
3342 g_return_if_fail (GTK_IS_SHEET (sheet));
3343 if (col >= xxx_column_count (sheet) || row >= yyy_row_count (sheet)) return;
3344 if (col < 0 || row < 0) return;
3346 gtk_sheet_get_attributes (sheet, row, col, &attributes);
3348 attributes.justification = justification;
3350 model = gtk_sheet_get_model (sheet);
3352 old_text = g_sheet_model_get_string (model, row, col);
3356 if (0 != safe_strcmp (old_text, text))
3357 changed = g_sheet_model_set_string (model, text, row, col);
3359 if ( g_sheet_model_free_strings (model))
3363 if (changed && attributes.is_visible)
3365 gchar *s = gtk_sheet_cell_get_text (sheet, row, col);
3367 if (s && strlen (s) > 0)
3369 text_width = STRING_WIDTH (GTK_WIDGET (sheet),
3370 attributes.font_desc, text);
3372 dispose_string (sheet, s);
3376 range.col0 = MIN_VISIBLE_COLUMN (sheet);
3377 range.coli = MAX_VISIBLE_COLUMN (sheet);
3379 if (gtk_sheet_autoresize (sheet) &&
3380 text_width > xxx_column_width (sheet, col) -
3381 2 * CELLOFFSET- attributes.border.width)
3383 gtk_sheet_set_column_width (sheet, col, text_width + 2 * CELLOFFSET
3384 + attributes.border.width);
3385 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_REDRAW_PENDING);
3388 if (!GTK_SHEET_IS_FROZEN (sheet))
3389 gtk_sheet_range_draw (sheet, &range);
3393 g_signal_emit (sheet, sheet_signals[CHANGED], 0, row, col);
3399 gtk_sheet_cell_clear (GtkSheet *sheet, gint row, gint column)
3401 GtkSheetRange range;
3403 g_return_if_fail (sheet != NULL);
3404 g_return_if_fail (GTK_IS_SHEET (sheet));
3405 if (column >= xxx_column_count (sheet) ||
3406 row >= yyy_row_count (sheet)) return;
3408 if (column < 0 || row < 0) return;
3412 range.col0 = MIN_VISIBLE_COLUMN (sheet);
3413 range.coli = MAX_VISIBLE_COLUMN (sheet);
3415 gtk_sheet_real_cell_clear (sheet, row, column);
3417 if (!GTK_SHEET_IS_FROZEN (sheet))
3419 gtk_sheet_range_draw (sheet, &range);
3424 gtk_sheet_real_cell_clear (GtkSheet *sheet, gint row, gint column)
3426 GSheetModel *model = gtk_sheet_get_model (sheet);
3428 gchar *old_text = gtk_sheet_cell_get_text (sheet, row, column);
3430 if (old_text && strlen (old_text) > 0 )
3432 g_sheet_model_datum_clear (model, row, column);
3435 dispose_string (sheet, old_text);
3439 gtk_sheet_range_clear (GtkSheet *sheet, const GtkSheetRange *range)
3441 g_return_if_fail (sheet != NULL);
3442 g_return_if_fail (GTK_IS_SHEET (sheet));
3444 gtk_sheet_real_range_clear (sheet, range);
3448 gtk_sheet_real_range_clear (GtkSheet *sheet, const GtkSheetRange *range)
3451 GtkSheetRange clear;
3456 clear.rowi = yyy_row_count (sheet) - 1;
3458 clear.coli = xxx_column_count (sheet) - 1;
3463 clear.row0 = MAX (clear.row0, 0);
3464 clear.col0 = MAX (clear.col0, 0);
3465 clear.rowi = MIN (clear.rowi, yyy_row_count (sheet) - 1 );
3466 clear.coli = MIN (clear.coli, xxx_column_count (sheet) - 1 );
3468 for (i = clear.row0; i <= clear.rowi; i++)
3469 for (j = clear.col0; j <= clear.coli; j++)
3471 gtk_sheet_real_cell_clear (sheet, i, j);
3474 gtk_sheet_range_draw (sheet, NULL);
3479 gtk_sheet_cell_empty (const GtkSheet *sheet, gint row, gint col)
3482 char *text = gtk_sheet_cell_get_text (sheet, row, col);
3483 empty = (text == NULL );
3485 dispose_string (sheet, text);
3492 gtk_sheet_cell_get_text (const GtkSheet *sheet, gint row, gint col)
3495 g_return_val_if_fail (sheet != NULL, NULL);
3496 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
3498 if (col >= xxx_column_count (sheet) || row >= yyy_row_count (sheet))
3500 if (col < 0 || row < 0) return NULL;
3502 model = gtk_sheet_get_model (sheet);
3507 return g_sheet_model_get_string (model, row, col);
3512 gtk_sheet_cell_get_state (GtkSheet *sheet, gint row, gint col)
3515 GtkSheetRange *range;
3517 g_return_val_if_fail (sheet != NULL, 0);
3518 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
3519 if (col >= xxx_column_count (sheet) || row >= yyy_row_count (sheet)) return 0;
3520 if (col < 0 || row < 0) return 0;
3522 state = sheet->state;
3523 range = &sheet->range;
3527 case GTK_SHEET_NORMAL:
3528 return GTK_STATE_NORMAL;
3530 case GTK_SHEET_ROW_SELECTED:
3531 if (row >= range->row0 && row <= range->rowi)
3532 return GTK_STATE_SELECTED;
3534 case GTK_SHEET_COLUMN_SELECTED:
3535 if (col >= range->col0 && col <= range->coli)
3536 return GTK_STATE_SELECTED;
3538 case GTK_SHEET_RANGE_SELECTED:
3539 if (row >= range->row0 && row <= range->rowi && \
3540 col >= range->col0 && col <= range->coli)
3541 return GTK_STATE_SELECTED;
3544 return GTK_STATE_NORMAL;
3547 /* Convert X, Y (in pixels) to *ROW, *COLUMN (in cell coords)
3548 -1 indicates the title buttons.
3549 If the function returns FALSE, then the results will be unreliable.
3552 gtk_sheet_get_pixel_info (GtkSheet *sheet,
3560 *column = -G_MAXINT;
3562 g_return_val_if_fail (sheet != NULL, 0);
3563 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
3565 /* bounds checking, return false if the user clicked
3573 if ( y < sheet->column_title_area.height + sheet->column_title_area.y)
3578 trow = ROW_FROM_YPIXEL (sheet, y);
3579 if (trow > yyy_row_count (sheet))
3585 if ( x < sheet->row_title_area.width + sheet->row_title_area.x)
3589 tcol = COLUMN_FROM_XPIXEL (sheet, x);
3590 if (tcol > xxx_column_count (sheet))
3600 gtk_sheet_get_cell_area (GtkSheet * sheet,
3605 g_return_val_if_fail (sheet != NULL, 0);
3606 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
3608 if (row >= yyy_row_count (sheet) || column >= xxx_column_count (sheet))
3611 area->x = (column == -1) ? 0 : (COLUMN_LEFT_XPIXEL (sheet, column) -
3612 (sheet->row_titles_visible
3613 ? sheet->row_title_area.width
3615 area->y = (row == -1) ? 0 : (ROW_TOP_YPIXEL (sheet, row) -
3616 (sheet->column_titles_visible
3617 ? sheet->column_title_area.height
3619 area->width= (column == -1) ? sheet->row_title_area.width
3620 : xxx_column_width (sheet, column);
3622 area->height= (row == -1) ? sheet->column_title_area.height
3623 : yyy_row_height (sheet, row);
3629 gtk_sheet_set_active_cell (GtkSheet *sheet, gint row, gint column)
3631 g_return_val_if_fail (sheet != NULL, 0);
3632 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
3634 if (row < - 1 || column < - 1) return FALSE;
3635 if (row >= yyy_row_count (sheet) || column >= xxx_column_count (sheet))
3638 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
3639 gtk_sheet_deactivate_cell (sheet);
3641 sheet->active_cell.row = row;
3642 sheet->active_cell.col = column;
3644 if ( row == -1 || column == -1)
3646 gtk_sheet_hide_active_cell (sheet);
3650 if (!gtk_sheet_activate_cell (sheet, row, column)) return FALSE;
3652 if (gtk_sheet_autoscroll (sheet))
3653 gtk_sheet_move_query (sheet, row, column);
3659 gtk_sheet_get_active_cell (GtkSheet *sheet, gint *row, gint *column)
3661 g_return_if_fail (sheet != NULL);
3662 g_return_if_fail (GTK_IS_SHEET (sheet));
3664 if ( row ) *row = sheet->active_cell.row;
3665 if (column) *column = sheet->active_cell.col;
3669 gtk_sheet_entry_changed (GtkWidget *widget, gpointer data)
3674 GtkJustification justification;
3675 GtkSheetCellAttr attributes;
3677 g_return_if_fail (data != NULL);
3678 g_return_if_fail (GTK_IS_SHEET (data));
3680 sheet = GTK_SHEET (data);
3682 if (!GTK_WIDGET_VISIBLE (widget)) return;
3683 if (sheet->state != GTK_STATE_NORMAL) return;
3685 row = sheet->active_cell.row;
3686 col = sheet->active_cell.col;
3688 if (row < 0 || col < 0) return;
3690 sheet->active_cell.row = -1;
3691 sheet->active_cell.col = -1;
3693 text = gtk_entry_get_text (GTK_ENTRY (gtk_sheet_get_entry (sheet)));
3695 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IS_FROZEN);
3697 if (text && strlen (text) > 0)
3699 gtk_sheet_get_attributes (sheet, row, col, &attributes);
3700 justification = attributes.justification;
3701 gtk_sheet_set_cell (sheet, row, col, justification, text);
3704 if (sheet->freeze_count == 0)
3705 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IS_FROZEN);
3707 sheet->active_cell.row = row;;
3708 sheet->active_cell.col = col;
3713 gtk_sheet_deactivate_cell (GtkSheet *sheet)
3715 g_return_if_fail (sheet != NULL);
3716 g_return_if_fail (GTK_IS_SHEET (sheet));
3718 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return ;
3719 if (sheet->state != GTK_SHEET_NORMAL) return ;
3721 if ( sheet->active_cell.row == -1 || sheet->active_cell.col == -1 )
3724 g_signal_emit (sheet, sheet_signals[DEACTIVATE], 0,
3725 sheet->active_cell.row,
3726 sheet->active_cell.col);
3729 g_signal_handlers_disconnect_by_func (gtk_sheet_get_entry (sheet),
3730 G_CALLBACK (gtk_sheet_entry_changed),
3733 gtk_sheet_hide_active_cell (sheet);
3734 sheet->active_cell.row = -1;
3735 sheet->active_cell.col = -1;
3737 if (GTK_SHEET_REDRAW_PENDING (sheet))
3739 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_REDRAW_PENDING);
3740 gtk_sheet_range_draw (sheet, NULL);
3745 gtk_sheet_hide_active_cell (GtkSheet *sheet)
3749 GtkJustification justification;
3750 GtkSheetCellAttr attributes;
3752 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
3754 row = sheet->active_cell.row;
3755 col = sheet->active_cell.col;
3757 if (row < 0 || col < 0) return;
3759 if (sheet->freeze_count == 0)
3760 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IS_FROZEN);
3762 text = gtk_entry_get_text (GTK_ENTRY (gtk_sheet_get_entry (sheet)));
3764 gtk_sheet_get_attributes (sheet, row, col, &attributes);
3765 justification = attributes.justification;
3767 row = sheet->active_cell.row;
3768 col = sheet->active_cell.col;
3770 gtk_widget_hide (sheet->entry_widget);
3771 gtk_widget_unmap (sheet->entry_widget);
3773 if (row != -1 && col != -1)
3774 gdk_draw_pixmap (sheet->sheet_window,
3775 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
3777 COLUMN_LEFT_XPIXEL (sheet, col)- 1,
3778 ROW_TOP_YPIXEL (sheet, row)- 1,
3779 COLUMN_LEFT_XPIXEL (sheet, col)- 1,
3780 ROW_TOP_YPIXEL (sheet, row)- 1,
3781 xxx_column_width (sheet, col) + 4,
3782 yyy_row_height (sheet, row)+4);
3784 gtk_widget_grab_focus (GTK_WIDGET (sheet));
3786 GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (sheet->entry_widget), GTK_VISIBLE);
3791 gtk_sheet_activate_cell (GtkSheet *sheet, gint row, gint col)
3793 gboolean veto = TRUE;
3795 g_return_val_if_fail (sheet != NULL, FALSE);
3796 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
3798 if (row < 0 || col < 0) return FALSE;
3800 if ( row > yyy_row_count (sheet) || col > xxx_column_count (sheet))
3803 if (!veto) return FALSE;
3804 if (sheet->state != GTK_SHEET_NORMAL)
3806 sheet->state = GTK_SHEET_NORMAL;
3807 gtk_sheet_real_unselect_range (sheet, NULL);
3810 sheet->range.row0 = row;
3811 sheet->range.col0 = col;
3812 sheet->range.rowi = row;
3813 sheet->range.coli = col;
3814 sheet->active_cell.row = row;
3815 sheet->active_cell.col = col;
3816 sheet->selection_cell.row = row;
3817 sheet->selection_cell.col = col;
3819 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3821 gtk_sheet_show_active_cell (sheet);
3823 g_signal_connect (gtk_sheet_get_entry (sheet),
3825 G_CALLBACK (gtk_sheet_entry_changed),
3828 _gtkextra_signal_emit (GTK_OBJECT (sheet), sheet_signals [ACTIVATE], row, col, &veto);
3834 gtk_sheet_show_active_cell (GtkSheet *sheet)
3836 GtkEntry *sheet_entry;
3837 GtkSheetCellAttr attributes;
3839 const gchar *old_text;
3840 GtkJustification justification;
3843 g_return_if_fail (sheet != NULL);
3844 g_return_if_fail (GTK_IS_SHEET (sheet));
3846 row = sheet->active_cell.row;
3847 col = sheet->active_cell.col;
3849 /* Don't show the active cell, if there is no active cell: */
3850 if (! (row >= 0 && col >= 0)) /* e.g row or coll == -1. */
3853 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
3854 if (sheet->state != GTK_SHEET_NORMAL) return;
3855 if (GTK_SHEET_IN_SELECTION (sheet)) return;
3857 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (sheet->entry_widget), GTK_VISIBLE);
3859 sheet_entry = GTK_ENTRY (gtk_sheet_get_entry (sheet));
3861 gtk_sheet_get_attributes (sheet, row, col, &attributes);
3863 justification = GTK_JUSTIFY_LEFT;
3865 if (gtk_sheet_justify_entry (sheet))
3866 justification = attributes.justification;
3868 text = gtk_sheet_cell_get_text (sheet, row, col);
3870 text = g_strdup ("");
3872 gtk_entry_set_visibility (GTK_ENTRY (sheet_entry), attributes.is_visible);
3875 /*** Added by John Gotts. Mar 25, 2005 *********/
3876 old_text = gtk_entry_get_text (GTK_ENTRY (sheet_entry));
3877 if (strcmp (old_text, text) != 0)
3879 if (!GTK_IS_ITEM_ENTRY (sheet_entry))
3880 gtk_entry_set_text (GTK_ENTRY (sheet_entry), text);
3882 gtk_item_entry_set_text (GTK_ITEM_ENTRY (sheet_entry), text, justification);
3885 gtk_sheet_entry_set_max_size (sheet);
3886 gtk_sheet_size_allocate_entry (sheet);
3888 gtk_widget_map (sheet->entry_widget);
3890 gtk_widget_grab_focus (GTK_WIDGET (sheet_entry));
3892 dispose_string (sheet, text);
3896 gtk_sheet_draw_active_cell (GtkSheet *sheet)
3899 GtkSheetRange range;
3901 if (!GTK_WIDGET_DRAWABLE (GTK_WIDGET (sheet))) return;
3902 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
3904 row = sheet->active_cell.row;
3905 col = sheet->active_cell.col;
3907 if (row < 0 || col < 0) return;
3909 if (!gtk_sheet_cell_isvisible (sheet, row, col)) return;
3911 range.col0 = range.coli = col;
3912 range.row0 = range.rowi = row;
3914 gtk_sheet_draw_border (sheet, range);
3919 gtk_sheet_make_backing_pixmap (GtkSheet *sheet, guint width, guint height)
3921 gint pixmap_width, pixmap_height;
3923 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
3925 if (width == 0 && height == 0)
3927 width = sheet->sheet_window_width + 80;
3928 height = sheet->sheet_window_height + 80;
3934 sheet->pixmap = gdk_pixmap_new (sheet->sheet_window,
3937 if (!GTK_SHEET_IS_FROZEN (sheet)) gtk_sheet_range_draw (sheet, NULL);
3941 /* reallocate if sizes don't match */
3942 gdk_window_get_size (sheet->pixmap,
3943 &pixmap_width, &pixmap_height);
3944 if ( (pixmap_width != width) || (pixmap_height != height))
3946 g_object_unref (sheet->pixmap);
3947 sheet->pixmap = gdk_pixmap_new (sheet->sheet_window,
3950 if (!GTK_SHEET_IS_FROZEN (sheet)) gtk_sheet_range_draw (sheet, NULL);
3956 gtk_sheet_new_selection (GtkSheet *sheet, GtkSheetRange *range)
3958 gint i, j, mask1, mask2;
3959 gint state, selected;
3960 gint x, y, width, height;
3961 GtkSheetRange new_range, aux_range;
3963 g_return_if_fail (sheet != NULL);
3965 if (range == NULL) range=&sheet->range;
3969 range->row0 = MIN (range->row0, sheet->range.row0);
3970 range->rowi = MAX (range->rowi, sheet->range.rowi);
3971 range->col0 = MIN (range->col0, sheet->range.col0);
3972 range->coli = MAX (range->coli, sheet->range.coli);
3974 range->row0 = MAX (range->row0, MIN_VISIBLE_ROW (sheet));
3975 range->rowi = MIN (range->rowi, MAX_VISIBLE_ROW (sheet));
3976 range->col0 = MAX (range->col0, MIN_VISIBLE_COLUMN (sheet));
3977 range->coli = MIN (range->coli, MAX_VISIBLE_COLUMN (sheet));
3979 aux_range.row0 = MAX (new_range.row0, MIN_VISIBLE_ROW (sheet));
3980 aux_range.rowi = MIN (new_range.rowi, MAX_VISIBLE_ROW (sheet));
3981 aux_range.col0 = MAX (new_range.col0, MIN_VISIBLE_COLUMN (sheet));
3982 aux_range.coli = MIN (new_range.coli, MAX_VISIBLE_COLUMN (sheet));
3984 for (i = range->row0; i <= range->rowi; i++)
3986 for (j = range->col0; j <= range->coli; j++)
3989 state = gtk_sheet_cell_get_state (sheet, i, j);
3990 selected= (i <= new_range.rowi && i >= new_range.row0 &&
3991 j <= new_range.coli && j >= new_range.col0) ? TRUE : FALSE;
3993 if (state == GTK_STATE_SELECTED && selected &&
3994 xxx_column_is_visible (sheet, j) && yyy_row_is_visible (sheet, i) &&
3995 (i == sheet->range.row0 || i == sheet->range.rowi ||
3996 j == sheet->range.col0 || j == sheet->range.coli ||
3997 i == new_range.row0 || i == new_range.rowi ||
3998 j == new_range.col0 || j == new_range.coli))
4001 mask1 = i == sheet->range.row0 ? 1 : 0;
4002 mask1 = i == sheet->range.rowi ? mask1 + 2 : mask1;
4003 mask1 = j == sheet->range.col0 ? mask1 + 4 : mask1;
4004 mask1 = j == sheet->range.coli ? mask1 + 8 : mask1;
4006 mask2 = i == new_range.row0 ? 1 : 0;
4007 mask2 = i == new_range.rowi ? mask2 + 2 : mask2;
4008 mask2 = j == new_range.col0 ? mask2 + 4 : mask2;
4009 mask2 = j == new_range.coli ? mask2 + 8 : mask2;
4013 x = COLUMN_LEFT_XPIXEL (sheet, j);
4014 y = ROW_TOP_YPIXEL (sheet, i);
4015 width = COLUMN_LEFT_XPIXEL (sheet, j)- x+
4016 xxx_column_width (sheet, j);
4017 height = ROW_TOP_YPIXEL (sheet, i)- y + yyy_row_height (sheet, i);
4019 if (i == sheet->range.row0)
4022 height = height + 3;
4024 if (i == sheet->range.rowi) height = height + 3;
4025 if (j == sheet->range.col0)
4030 if (j == sheet->range.coli) width = width + 3;
4032 gdk_draw_pixmap (sheet->sheet_window,
4033 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
4042 if (i != sheet->active_cell.row || j != sheet->active_cell.col)
4044 x = COLUMN_LEFT_XPIXEL (sheet, j);
4045 y = ROW_TOP_YPIXEL (sheet, i);
4046 width = COLUMN_LEFT_XPIXEL (sheet, j)- x+
4047 xxx_column_width (sheet, j);
4049 height = ROW_TOP_YPIXEL (sheet, i)- y + yyy_row_height (sheet, i);
4051 if (i == new_range.row0)
4054 height = height - 2;
4056 if (i == new_range.rowi) height = height - 3;
4057 if (j == new_range.col0)
4062 if (j == new_range.coli) width = width - 3;
4064 gdk_draw_rectangle (sheet->sheet_window,
4075 for (i = range->row0; i <= range->rowi; i++)
4077 for (j = range->col0; j <= range->coli; j++)
4080 state = gtk_sheet_cell_get_state (sheet, i, j);
4081 selected= (i <= new_range.rowi && i >= new_range.row0 &&
4082 j <= new_range.coli && j >= new_range.col0) ? TRUE : FALSE;
4084 if (state == GTK_STATE_SELECTED && !selected &&
4085 xxx_column_is_visible (sheet, j) && yyy_row_is_visible (sheet, i))
4088 x = COLUMN_LEFT_XPIXEL (sheet, j);
4089 y = ROW_TOP_YPIXEL (sheet, i);
4090 width = COLUMN_LEFT_XPIXEL (sheet, j)- x+ xxx_column_width (sheet, j);
4091 height = ROW_TOP_YPIXEL (sheet, i)- y + yyy_row_height (sheet, i);
4093 if (i == sheet->range.row0)
4096 height = height + 3;
4098 if (i == sheet->range.rowi) height = height + 3;
4099 if (j == sheet->range.col0)
4104 if (j == sheet->range.coli) width = width + 3;
4106 gdk_draw_pixmap (sheet->sheet_window,
4107 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
4119 for (i = range->row0; i <= range->rowi; i++)
4121 for (j = range->col0; j <= range->coli; j++)
4124 state = gtk_sheet_cell_get_state (sheet, i, j);
4125 selected= (i <= new_range.rowi && i >= new_range.row0 &&
4126 j <= new_range.coli && j >= new_range.col0) ? TRUE : FALSE;
4128 if (state != GTK_STATE_SELECTED && selected &&
4129 xxx_column_is_visible (sheet, j) && yyy_row_is_visible (sheet, i) &&
4130 (i != sheet->active_cell.row || j != sheet->active_cell.col))
4133 x = COLUMN_LEFT_XPIXEL (sheet, j);
4134 y = ROW_TOP_YPIXEL (sheet, i);
4135 width = COLUMN_LEFT_XPIXEL (sheet, j)- x+ xxx_column_width (sheet, j);
4136 height = ROW_TOP_YPIXEL (sheet, i)- y + yyy_row_height (sheet, i);
4138 if (i == new_range.row0)
4141 height = height - 2;
4143 if (i == new_range.rowi) height = height - 3;
4144 if (j == new_range.col0)
4149 if (j == new_range.coli) width = width - 3;
4151 gdk_draw_rectangle (sheet->sheet_window,
4162 for (i = aux_range.row0; i <= aux_range.rowi; i++)
4164 for (j = aux_range.col0; j <= aux_range.coli; j++)
4167 if (xxx_column_is_visible (sheet, j) && yyy_row_is_visible (sheet, i))
4170 state = gtk_sheet_cell_get_state (sheet, i, j);
4172 mask1 = i == sheet->range.row0 ? 1 : 0;
4173 mask1 = i == sheet->range.rowi ? mask1 + 2 : mask1;
4174 mask1 = j == sheet->range.col0 ? mask1 + 4 : mask1;
4175 mask1 = j == sheet->range.coli ? mask1 + 8 : mask1;
4177 mask2 = i == new_range.row0 ? 1 : 0;
4178 mask2 = i == new_range.rowi ? mask2 + 2 : mask2;
4179 mask2 = j == new_range.col0 ? mask2 + 4 : mask2;
4180 mask2 = j == new_range.coli ? mask2 + 8 : mask2;
4181 if (mask2 != mask1 || (mask2 == mask1 && state != GTK_STATE_SELECTED))
4183 x = COLUMN_LEFT_XPIXEL (sheet, j);
4184 y = ROW_TOP_YPIXEL (sheet, i);
4185 width = xxx_column_width (sheet, j);
4186 height = yyy_row_height (sheet, i);
4188 gdk_draw_rectangle (sheet->sheet_window,
4196 gdk_draw_rectangle (sheet->sheet_window,
4199 x + 1, y + height - 1,
4203 gdk_draw_rectangle (sheet->sheet_window,
4211 gdk_draw_rectangle (sheet->sheet_window,
4214 x + width - 1, y + 1,
4228 gtk_sheet_draw_corners (sheet, new_range);
4233 gtk_sheet_draw_border (GtkSheet *sheet, GtkSheetRange new_range)
4238 gint x, y, width, height;
4240 widget = GTK_WIDGET (sheet);
4242 x = COLUMN_LEFT_XPIXEL (sheet, new_range.col0);
4243 y = ROW_TOP_YPIXEL (sheet, new_range.row0);
4244 width = COLUMN_LEFT_XPIXEL (sheet, new_range.coli) - x +
4245 xxx_column_width (sheet, new_range.coli);
4247 height = ROW_TOP_YPIXEL (sheet, new_range.rowi) - y +
4248 yyy_row_height (sheet, new_range.rowi);
4250 area.x = COLUMN_LEFT_XPIXEL (sheet, MIN_VISIBLE_COLUMN (sheet));
4251 area.y = ROW_TOP_YPIXEL (sheet, MIN_VISIBLE_ROW (sheet));
4252 area.width = sheet->sheet_window_width;
4253 area.height = sheet->sheet_window_height;
4260 if (width > area.width) width = area.width + 10;
4263 height = height + y;
4266 if (height > area.height) height = area.height + 10;
4268 gdk_gc_set_clip_rectangle (sheet->xor_gc, &area);
4270 for (i = -1; i <= 1; ++i)
4271 gdk_draw_rectangle (sheet->sheet_window,
4279 gdk_gc_set_clip_rectangle (sheet->xor_gc, NULL);
4282 gtk_sheet_draw_corners (sheet, new_range);
4286 gtk_sheet_draw_corners (GtkSheet *sheet, GtkSheetRange range)
4291 if (gtk_sheet_cell_isvisible (sheet, range.row0, range.col0))
4293 x = COLUMN_LEFT_XPIXEL (sheet, range.col0);
4294 y = ROW_TOP_YPIXEL (sheet, range.row0);
4295 gdk_draw_pixmap (sheet->sheet_window,
4296 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
4304 gdk_draw_rectangle (sheet->sheet_window,
4311 if (gtk_sheet_cell_isvisible (sheet, range.row0, range.coli) ||
4312 sheet->state == GTK_SHEET_COLUMN_SELECTED)
4314 x = COLUMN_LEFT_XPIXEL (sheet, range.coli)+
4315 xxx_column_width (sheet, range.coli);
4316 y = ROW_TOP_YPIXEL (sheet, range.row0);
4318 if (sheet->state == GTK_SHEET_COLUMN_SELECTED)
4320 y = ROW_TOP_YPIXEL (sheet, MIN_VISIBLE_ROW (sheet))+3;
4323 gdk_draw_pixmap (sheet->sheet_window,
4324 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
4332 gdk_draw_rectangle (sheet->sheet_window,
4335 x - width + width / 2, y - width + width / 2,
4336 2 + width, 2 + width);
4339 if (gtk_sheet_cell_isvisible (sheet, range.rowi, range.col0) ||
4340 sheet->state == GTK_SHEET_ROW_SELECTED)
4342 x = COLUMN_LEFT_XPIXEL (sheet, range.col0);
4343 y = ROW_TOP_YPIXEL (sheet, range.rowi)+
4344 yyy_row_height (sheet, range.rowi);
4346 if (sheet->state == GTK_SHEET_ROW_SELECTED)
4348 x = COLUMN_LEFT_XPIXEL (sheet, MIN_VISIBLE_COLUMN (sheet))+3;
4351 gdk_draw_pixmap (sheet->sheet_window,
4352 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
4360 gdk_draw_rectangle (sheet->sheet_window,
4363 x - width + width / 2, y - width + width / 2,
4364 2 + width, 2 + width);
4367 if (gtk_sheet_cell_isvisible (sheet, range.rowi, range.coli))
4369 x = COLUMN_LEFT_XPIXEL (sheet, range.coli)+
4370 xxx_column_width (sheet, range.coli);
4371 y = ROW_TOP_YPIXEL (sheet, range.rowi)+
4372 yyy_row_height (sheet, range.rowi);
4374 if (sheet->state == GTK_SHEET_RANGE_SELECTED) width = 3;
4375 if (sheet->state == GTK_SHEET_NORMAL) width = 3;
4376 gdk_draw_pixmap (sheet->sheet_window,
4377 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
4385 gdk_draw_rectangle (sheet->sheet_window,
4388 x - width + width / 2, y - width + width / 2,
4389 2 + width, 2 + width);
4397 gtk_sheet_real_select_range (GtkSheet * sheet,
4398 const GtkSheetRange * range)
4402 g_return_if_fail (sheet != NULL);
4404 if (range == NULL) range = &sheet->range;
4406 memcpy (&sheet->range, range, sizeof (*range));
4408 if (range->row0 < 0 || range->rowi < 0) return;
4409 if (range->col0 < 0 || range->coli < 0) return;
4411 state = sheet->state;
4413 if (range->coli != sheet->range.coli || range->col0 != sheet->range.col0 ||
4414 range->rowi != sheet->range.rowi || range->row0 != sheet->range.row0)
4416 gtk_sheet_new_selection (sheet, &sheet->range);
4420 gtk_sheet_draw_backing_pixmap (sheet, sheet->range);
4421 gtk_sheet_range_draw_selection (sheet, sheet->range);
4424 gtk_sheet_update_primary_selection (sheet);
4426 g_signal_emit (sheet, sheet_signals[SELECT_RANGE], 0, &sheet->range);
4431 gtk_sheet_get_selected_range (GtkSheet *sheet,
4432 GtkSheetRange *range)
4434 g_return_if_fail (sheet != NULL);
4435 *range = sheet->range;
4440 gtk_sheet_select_range (GtkSheet * sheet, const GtkSheetRange *range)
4442 g_return_if_fail (sheet != NULL);
4444 if (range == NULL) range=&sheet->range;
4446 if (range->row0 < 0 || range->rowi < 0) return;
4447 if (range->col0 < 0 || range->coli < 0) return;
4450 if (sheet->state != GTK_SHEET_NORMAL)
4451 gtk_sheet_real_unselect_range (sheet, NULL);
4453 gtk_sheet_deactivate_cell (sheet);
4455 sheet->range.row0 = range->row0;
4456 sheet->range.rowi = range->rowi;
4457 sheet->range.col0 = range->col0;
4458 sheet->range.coli = range->coli;
4459 sheet->active_cell.row = range->row0;
4460 sheet->active_cell.col = range->col0;
4461 sheet->selection_cell.row = range->rowi;
4462 sheet->selection_cell.col = range->coli;
4464 sheet->state = GTK_SHEET_RANGE_SELECTED;
4465 gtk_sheet_real_select_range (sheet, NULL);
4470 gtk_sheet_unselect_range (GtkSheet * sheet)
4472 if (! GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
4475 gtk_sheet_real_unselect_range (sheet, NULL);
4476 sheet->state = GTK_STATE_NORMAL;
4478 gtk_sheet_activate_cell (sheet,
4479 sheet->active_cell.row, sheet->active_cell.col);
4484 gtk_sheet_real_unselect_range (GtkSheet * sheet,
4485 const GtkSheetRange *range)
4487 g_return_if_fail (sheet != NULL);
4488 g_return_if_fail (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)));
4491 range = &sheet->range;
4493 if (range->row0 < 0 || range->rowi < 0) return;
4494 if (range->col0 < 0 || range->coli < 0) return;
4496 g_signal_emit (sheet, sheet_signals[SELECT_COLUMN], 0, -1);
4497 g_signal_emit (sheet, sheet_signals[SELECT_ROW], 0, -1);
4499 if (gtk_sheet_range_isvisible (sheet, *range))
4500 gtk_sheet_draw_backing_pixmap (sheet, *range);
4502 sheet->range.row0 = -1;
4503 sheet->range.rowi = -1;
4504 sheet->range.col0 = -1;
4505 sheet->range.coli = -1;
4507 gtk_sheet_position_children (sheet);
4512 gtk_sheet_expose (GtkWidget * widget,
4513 GdkEventExpose * event)
4516 GtkSheetRange range;
4518 g_return_val_if_fail (widget != NULL, FALSE);
4519 g_return_val_if_fail (GTK_IS_SHEET (widget), FALSE);
4520 g_return_val_if_fail (event != NULL, FALSE);
4523 sheet = GTK_SHEET (widget);
4525 if (GTK_WIDGET_DRAWABLE (widget))
4527 range.row0 = ROW_FROM_YPIXEL (sheet, event->area.y);
4528 range.col0 = COLUMN_FROM_XPIXEL (sheet, event->area.x);
4529 range.rowi = ROW_FROM_YPIXEL (sheet, event->area.y + event->area.height);
4530 range.coli = COLUMN_FROM_XPIXEL (sheet, event->area.x + event->area.width);
4532 /* exposure events on the sheet */
4533 if (event->window == sheet->row_title_window &&
4534 sheet->row_titles_visible)
4537 for (i = MIN_VISIBLE_ROW (sheet); i <= MAX_VISIBLE_ROW (sheet); i++)
4538 gtk_sheet_row_title_button_draw (sheet, i);
4541 if (event->window == sheet->column_title_window &&
4542 sheet->column_titles_visible)
4545 for (i = MIN_VISIBLE_COLUMN (sheet); i <= MAX_VISIBLE_COLUMN (sheet); i++)
4546 gtk_sheet_column_title_button_draw (sheet, i);
4549 if (event->window == sheet->sheet_window)
4551 gtk_sheet_draw_backing_pixmap (sheet, range);
4553 if (sheet->state != GTK_SHEET_NORMAL)
4555 if (gtk_sheet_range_isvisible (sheet, sheet->range))
4556 gtk_sheet_draw_backing_pixmap (sheet, sheet->range);
4557 if (GTK_SHEET_IN_RESIZE (sheet) || GTK_SHEET_IN_DRAG (sheet))
4558 gtk_sheet_draw_backing_pixmap (sheet, sheet->drag_range);
4560 if (gtk_sheet_range_isvisible (sheet, sheet->range))
4561 gtk_sheet_range_draw_selection (sheet, sheet->range);
4562 if (GTK_SHEET_IN_RESIZE (sheet) || GTK_SHEET_IN_DRAG (sheet))
4563 draw_xor_rectangle (sheet, sheet->drag_range);
4566 if ((!GTK_SHEET_IN_XDRAG (sheet)) && (!GTK_SHEET_IN_YDRAG (sheet)))
4568 if (sheet->state == GTK_SHEET_NORMAL)
4569 gtk_sheet_draw_active_cell (sheet);
4574 if (sheet->state != GTK_SHEET_NORMAL && GTK_SHEET_IN_SELECTION (sheet))
4575 gtk_widget_grab_focus (GTK_WIDGET (sheet));
4577 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
4584 gtk_sheet_button_press (GtkWidget * widget,
4585 GdkEventButton * event)
4588 GdkModifierType mods;
4589 gint x, y, row, column;
4592 g_return_val_if_fail (widget != NULL, FALSE);
4593 g_return_val_if_fail (GTK_IS_SHEET (widget), FALSE);
4594 g_return_val_if_fail (event != NULL, FALSE);
4596 sheet = GTK_SHEET (widget);
4598 /* Cancel any pending tooltips */
4599 if (sheet->motion_timer)
4601 g_source_remove (sheet->motion_timer);
4602 sheet->motion_timer = 0;
4605 gtk_widget_get_pointer (widget, &x, &y);
4606 gtk_sheet_get_pixel_info (sheet, x, y, &row, &column);
4609 if (event->window == sheet->column_title_window)
4611 g_signal_emit (sheet,
4612 sheet_signals[BUTTON_EVENT_COLUMN], 0,
4615 if ( event->type == GDK_2BUTTON_PRESS && event->button == 1)
4616 g_signal_emit (sheet,
4617 sheet_signals[DOUBLE_CLICK_COLUMN], 0, column);
4620 else if (event->window == sheet->row_title_window)
4622 g_signal_emit (sheet,
4623 sheet_signals[BUTTON_EVENT_ROW], 0,
4626 if ( event->type == GDK_2BUTTON_PRESS && event->button == 1)
4627 g_signal_emit (sheet,
4628 sheet_signals[DOUBLE_CLICK_ROW], 0, row);
4632 gdk_window_get_pointer (widget->window, NULL, NULL, &mods);
4634 if (! (mods & GDK_BUTTON1_MASK)) return TRUE;
4637 /* press on resize windows */
4638 if (event->window == sheet->column_title_window &&
4639 gtk_sheet_columns_resizable (sheet))
4641 gtk_widget_get_pointer (widget, &sheet->x_drag, NULL);
4642 if (POSSIBLE_XDRAG (sheet, sheet->x_drag, &sheet->drag_cell.col))
4645 if (event->type == GDK_2BUTTON_PRESS)
4647 gtk_sheet_autoresize_column (sheet, sheet->drag_cell.col);
4648 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_XDRAG);
4651 gtk_sheet_column_size_request (sheet, sheet->drag_cell.col, &req);
4652 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_XDRAG);
4653 gdk_pointer_grab (sheet->column_title_window, FALSE,
4654 GDK_POINTER_MOTION_HINT_MASK |
4655 GDK_BUTTON1_MOTION_MASK |
4656 GDK_BUTTON_RELEASE_MASK,
4657 NULL, NULL, event->time);
4659 draw_xor_vline (sheet);
4664 if (event->window == sheet->row_title_window && gtk_sheet_rows_resizable (sheet))
4666 gtk_widget_get_pointer (widget, NULL, &sheet->y_drag);
4668 if (POSSIBLE_YDRAG (sheet, sheet->y_drag, &sheet->drag_cell.row))
4671 gtk_sheet_row_size_request (sheet, sheet->drag_cell.row, &req);
4672 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_YDRAG);
4673 gdk_pointer_grab (sheet->row_title_window, FALSE,
4674 GDK_POINTER_MOTION_HINT_MASK |
4675 GDK_BUTTON1_MOTION_MASK |
4676 GDK_BUTTON_RELEASE_MASK,
4677 NULL, NULL, event->time);
4679 draw_xor_hline (sheet);
4684 /* the sheet itself does not handle other than single click events */
4685 if (event->type != GDK_BUTTON_PRESS) return FALSE;
4687 /* selections on the sheet */
4688 if (event->window == sheet->sheet_window)
4690 gtk_widget_get_pointer (widget, &x, &y);
4691 gtk_sheet_get_pixel_info (sheet, x, y, &row, &column);
4692 gdk_pointer_grab (sheet->sheet_window, FALSE,
4693 GDK_POINTER_MOTION_HINT_MASK |
4694 GDK_BUTTON1_MOTION_MASK |
4695 GDK_BUTTON_RELEASE_MASK,
4696 NULL, NULL, event->time);
4697 gtk_grab_add (GTK_WIDGET (sheet));
4699 /* This seems to be a kludge to work around a problem where the sheet
4700 scrolls to another position. The timeout scrolls it back to its
4701 original posn. JMD 3 July 2007
4703 gtk_widget_grab_focus (GTK_WIDGET (sheet));
4705 if (sheet->selection_mode != GTK_SELECTION_SINGLE &&
4706 sheet->selection_mode != GTK_SELECTION_NONE &&
4707 sheet->cursor_drag->type == GDK_SIZING &&
4708 !GTK_SHEET_IN_SELECTION (sheet) && !GTK_SHEET_IN_RESIZE (sheet))
4710 if (sheet->state == GTK_STATE_NORMAL)
4712 row = sheet->active_cell.row;
4713 column = sheet->active_cell.col;
4714 gtk_sheet_deactivate_cell (sheet);
4715 sheet->active_cell.row = row;
4716 sheet->active_cell.col = column;
4717 sheet->drag_range = sheet->range;
4718 sheet->state = GTK_SHEET_RANGE_SELECTED;
4719 gtk_sheet_select_range (sheet, &sheet->drag_range);
4723 if (row > sheet->range.rowi) row--;
4724 if (column > sheet->range.coli) column--;
4725 sheet->drag_cell.row = row;
4726 sheet->drag_cell.col = column;
4727 sheet->drag_range = sheet->range;
4728 draw_xor_rectangle (sheet, sheet->drag_range);
4729 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_RESIZE);
4731 else if (sheet->cursor_drag->type == GDK_TOP_LEFT_ARROW &&
4732 !GTK_SHEET_IN_SELECTION (sheet)
4733 && ! GTK_SHEET_IN_DRAG (sheet)
4734 && sheet->active_cell.row >= 0
4735 && sheet->active_cell.col >= 0
4738 if (sheet->state == GTK_STATE_NORMAL)
4740 row = sheet->active_cell.row;
4741 column = sheet->active_cell.col;
4742 gtk_sheet_deactivate_cell (sheet);
4743 sheet->active_cell.row = row;
4744 sheet->active_cell.col = column;
4745 sheet->drag_range = sheet->range;
4746 sheet->state = GTK_SHEET_RANGE_SELECTED;
4747 gtk_sheet_select_range (sheet, &sheet->drag_range);
4751 if (row < sheet->range.row0) row++;
4752 if (row > sheet->range.rowi) row--;
4753 if (column < sheet->range.col0) column++;
4754 if (column > sheet->range.coli) column--;
4755 sheet->drag_cell.row = row;
4756 sheet->drag_cell.col = column;
4757 sheet->drag_range = sheet->range;
4758 draw_xor_rectangle (sheet, sheet->drag_range);
4759 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_DRAG);
4763 gtk_sheet_click_cell (sheet, row, column, &veto);
4764 if (veto) GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4768 if (event->window == sheet->column_title_window)
4770 gtk_widget_get_pointer (widget, &x, &y);
4771 column = COLUMN_FROM_XPIXEL (sheet, x);
4773 if (xxx_column_is_sensitive (sheet, column))
4775 gtk_sheet_click_cell (sheet, - 1, column, &veto);
4776 gtk_grab_add (GTK_WIDGET (sheet));
4777 gtk_widget_grab_focus (GTK_WIDGET (sheet));
4778 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4782 if (event->window == sheet->row_title_window)
4784 gtk_widget_get_pointer (widget, &x, &y);
4785 row = ROW_FROM_YPIXEL (sheet, y);
4786 if (yyy_row_is_sensitive (sheet, row))
4788 gtk_sheet_click_cell (sheet, row, - 1, &veto);
4789 gtk_grab_add (GTK_WIDGET (sheet));
4790 gtk_widget_grab_focus (GTK_WIDGET (sheet));
4791 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4799 gtk_sheet_click_cell (GtkSheet *sheet, gint row, gint column, gboolean *veto)
4803 if (row >= yyy_row_count (sheet) || column >= xxx_column_count (sheet))
4809 if (column >= 0 && row >= 0)
4810 if (! xxx_column_is_visible (sheet, column) || !yyy_row_is_visible (sheet, row))
4816 _gtkextra_signal_emit (GTK_OBJECT (sheet), sheet_signals[TRAVERSE],
4817 sheet->active_cell.row, sheet->active_cell.col,
4818 &row, &column, veto);
4822 if (sheet->state == GTK_STATE_NORMAL) return;
4824 row = sheet->active_cell.row;
4825 column = sheet->active_cell.col;
4827 gtk_sheet_activate_cell (sheet, row, column);
4831 if (row == -1 && column >= 0)
4833 if (gtk_sheet_autoscroll (sheet))
4834 gtk_sheet_move_query (sheet, row, column);
4835 gtk_sheet_select_column (sheet, column);
4838 if (column == -1 && row >= 0)
4840 if (gtk_sheet_autoscroll (sheet))
4841 gtk_sheet_move_query (sheet, row, column);
4842 gtk_sheet_select_row (sheet, row);
4846 if (row == - 1 && column == - 1)
4848 sheet->range.row0 = 0;
4849 sheet->range.col0 = 0;
4850 sheet->range.rowi = yyy_row_count (sheet) - 1;
4851 sheet->range.coli = xxx_column_count (sheet) - 1;
4852 sheet->active_cell.row = 0;
4853 sheet->active_cell.col = 0;
4854 gtk_sheet_select_range (sheet, NULL);
4858 if (row != -1 && column != -1)
4860 if (sheet->state != GTK_SHEET_NORMAL)
4862 sheet->state = GTK_SHEET_NORMAL;
4863 gtk_sheet_real_unselect_range (sheet, NULL);
4867 gtk_sheet_deactivate_cell (sheet);
4868 gtk_sheet_activate_cell (sheet, row, column);
4871 if (gtk_sheet_autoscroll (sheet))
4872 gtk_sheet_move_query (sheet, row, column);
4873 sheet->active_cell.row = row;
4874 sheet->active_cell.col = column;
4875 sheet->selection_cell.row = row;
4876 sheet->selection_cell.col = column;
4877 sheet->range.row0 = row;
4878 sheet->range.col0 = column;
4879 sheet->range.rowi = row;
4880 sheet->range.coli = column;
4881 sheet->state = GTK_SHEET_NORMAL;
4882 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4883 gtk_sheet_draw_active_cell (sheet);
4887 g_assert_not_reached ();
4888 gtk_sheet_activate_cell (sheet, sheet->active_cell.row,
4889 sheet->active_cell.col);
4893 gtk_sheet_button_release (GtkWidget * widget,
4894 GdkEventButton * event)
4899 sheet = GTK_SHEET (widget);
4901 /* release on resize windows */
4902 if (GTK_SHEET_IN_XDRAG (sheet))
4904 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_XDRAG);
4905 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4906 gtk_widget_get_pointer (widget, &x, NULL);
4907 gdk_pointer_ungrab (event->time);
4908 draw_xor_vline (sheet);
4910 gtk_sheet_set_column_width (sheet, sheet->drag_cell.col,
4911 new_column_width (sheet, sheet->drag_cell.col, &x));
4912 sheet->old_hadjustment = -1.;
4913 g_signal_emit_by_name (sheet->hadjustment, "value_changed");
4917 if (GTK_SHEET_IN_YDRAG (sheet))
4919 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_YDRAG);
4920 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4921 gtk_widget_get_pointer (widget, NULL, &y);
4922 gdk_pointer_ungrab (event->time);
4923 draw_xor_hline (sheet);
4925 gtk_sheet_set_row_height (sheet, sheet->drag_cell.row, new_row_height (sheet, sheet->drag_cell.row, &y));
4926 sheet->old_vadjustment = -1.;
4927 g_signal_emit_by_name (sheet->vadjustment, "value_changed");
4932 if (GTK_SHEET_IN_DRAG (sheet))
4934 GtkSheetRange old_range;
4935 draw_xor_rectangle (sheet, sheet->drag_range);
4936 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_DRAG);
4937 gdk_pointer_ungrab (event->time);
4939 gtk_sheet_real_unselect_range (sheet, NULL);
4941 sheet->active_cell.row = sheet->active_cell.row +
4942 (sheet->drag_range.row0 - sheet->range.row0);
4943 sheet->active_cell.col = sheet->active_cell.col +
4944 (sheet->drag_range.col0 - sheet->range.col0);
4945 sheet->selection_cell.row = sheet->selection_cell.row +
4946 (sheet->drag_range.row0 - sheet->range.row0);
4947 sheet->selection_cell.col = sheet->selection_cell.col +
4948 (sheet->drag_range.col0 - sheet->range.col0);
4949 old_range = sheet->range;
4950 sheet->range = sheet->drag_range;
4951 sheet->drag_range = old_range;
4952 g_signal_emit (sheet, sheet_signals[MOVE_RANGE], 0,
4953 &sheet->drag_range, &sheet->range);
4954 gtk_sheet_select_range (sheet, &sheet->range);
4957 if (GTK_SHEET_IN_RESIZE (sheet))
4959 GtkSheetRange old_range;
4960 draw_xor_rectangle (sheet, sheet->drag_range);
4961 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_RESIZE);
4962 gdk_pointer_ungrab (event->time);
4964 gtk_sheet_real_unselect_range (sheet, NULL);
4966 sheet->active_cell.row = sheet->active_cell.row +
4967 (sheet->drag_range.row0 - sheet->range.row0);
4968 sheet->active_cell.col = sheet->active_cell.col +
4969 (sheet->drag_range.col0 - sheet->range.col0);
4970 if (sheet->drag_range.row0 < sheet->range.row0)
4971 sheet->selection_cell.row = sheet->drag_range.row0;
4972 if (sheet->drag_range.rowi >= sheet->range.rowi)
4973 sheet->selection_cell.row = sheet->drag_range.rowi;
4974 if (sheet->drag_range.col0 < sheet->range.col0)
4975 sheet->selection_cell.col = sheet->drag_range.col0;
4976 if (sheet->drag_range.coli >= sheet->range.coli)
4977 sheet->selection_cell.col = sheet->drag_range.coli;
4978 old_range = sheet->range;
4979 sheet->range = sheet->drag_range;
4980 sheet->drag_range = old_range;
4982 if (sheet->state == GTK_STATE_NORMAL) sheet->state = GTK_SHEET_RANGE_SELECTED;
4983 g_signal_emit (sheet, sheet_signals[RESIZE_RANGE], 0,
4984 &sheet->drag_range, &sheet->range);
4985 gtk_sheet_select_range (sheet, &sheet->range);
4988 if (sheet->state == GTK_SHEET_NORMAL && GTK_SHEET_IN_SELECTION (sheet))
4990 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4991 gdk_pointer_ungrab (event->time);
4992 gtk_sheet_activate_cell (sheet, sheet->active_cell.row,
4993 sheet->active_cell.col);
4996 if (GTK_SHEET_IN_SELECTION)
4997 gdk_pointer_ungrab (event->time);
4998 gtk_grab_remove (GTK_WIDGET (sheet));
5000 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
5005 /* Shamelessly lifted from gtktooltips */
5007 gtk_sheet_subtitle_paint_window (GtkWidget *tip_window)
5011 gtk_widget_size_request (tip_window, &req);
5012 gtk_paint_flat_box (tip_window->style, tip_window->window,
5013 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
5014 NULL, GTK_WIDGET(tip_window), "tooltip",
5015 0, 0, req.width, req.height);
5020 static GtkSheetHoverTitle *
5021 create_hover_window (void)
5023 GtkSheetHoverTitle *hw = malloc (sizeof (*hw));
5025 hw->window = gtk_window_new (GTK_WINDOW_POPUP);
5027 #if GTK_CHECK_VERSION (2, 9, 0)
5028 gtk_window_set_type_hint (GTK_WINDOW (hw->window),
5029 GDK_WINDOW_TYPE_HINT_TOOLTIP);
5032 gtk_widget_set_app_paintable (hw->window, TRUE);
5033 gtk_window_set_resizable (GTK_WINDOW (hw->window), FALSE);
5034 gtk_widget_set_name (hw->window, "gtk-tooltips");
5035 gtk_container_set_border_width (GTK_CONTAINER (hw->window), 4);
5037 g_signal_connect (hw->window,
5039 G_CALLBACK (gtk_sheet_subtitle_paint_window),
5042 hw->label = gtk_label_new (NULL);
5045 gtk_label_set_line_wrap (GTK_LABEL (hw->label), TRUE);
5046 gtk_misc_set_alignment (GTK_MISC (hw->label), 0.5, 0.5);
5048 gtk_container_add (GTK_CONTAINER (hw->window), hw->label);
5050 gtk_widget_show (hw->label);
5052 g_signal_connect (hw->window,
5054 G_CALLBACK (gtk_widget_destroyed),
5060 #define HOVER_WINDOW_Y_OFFSET 2
5063 show_subtitle (GtkSheet *sheet, gint row, gint column, const gchar *subtitle)
5072 if ( ! sheet->hover_window)
5074 sheet->hover_window = create_hover_window ();
5075 gtk_widget_add_events (GTK_WIDGET (sheet), GDK_LEAVE_NOTIFY_MASK);
5077 g_signal_connect_swapped (sheet, "leave-notify-event",
5078 G_CALLBACK (gtk_widget_hide),
5079 sheet->hover_window->window);
5082 gtk_label_set_text (GTK_LABEL (sheet->hover_window->label),
5086 sheet->hover_window->row = row;
5087 sheet->hover_window->column = column;
5089 gdk_window_get_origin (GTK_WIDGET (sheet)->window, &x, &y);
5091 gtk_widget_get_pointer (GTK_WIDGET (sheet), &px, &py);
5093 gtk_widget_show (sheet->hover_window->window);
5095 width = GTK_WIDGET (sheet->hover_window->label)->allocation.width;
5101 y += sheet->column_title_area.y;
5102 y += sheet->column_title_area.height;
5103 y += HOVER_WINDOW_Y_OFFSET;
5109 x += sheet->row_title_area.x;
5110 x += sheet->row_title_area.width * 2 / 3.0;
5113 gtk_window_move (GTK_WINDOW (sheet->hover_window->window),
5118 motion_timeout_callback (gpointer data)
5120 GtkSheet *sheet = GTK_SHEET (data);
5123 gtk_widget_get_pointer (GTK_WIDGET (sheet), &x, &y);
5125 if ( gtk_sheet_get_pixel_info (sheet, x, y, &row, &column) )
5127 if ( column == -1 && row == -1 )
5132 GSheetRow *row_geo = sheet->row_geometry;
5135 text = g_sheet_row_get_subtitle (row_geo, row);
5137 show_subtitle (sheet, row, column, text);
5143 GSheetColumn *col_geo = sheet->column_geometry;
5146 text = g_sheet_column_get_subtitle (col_geo, column);
5148 show_subtitle (sheet, row, column, text );
5158 gtk_sheet_motion (GtkWidget * widget,
5159 GdkEventMotion * event)
5162 GdkModifierType mods;
5163 GdkCursorType new_cursor;
5167 g_return_val_if_fail (widget != NULL, FALSE);
5168 g_return_val_if_fail (GTK_IS_SHEET (widget), FALSE);
5169 g_return_val_if_fail (event != NULL, FALSE);
5171 sheet = GTK_SHEET (widget);
5173 /* selections on the sheet */
5177 if (!sheet->hover_window || ! GTK_WIDGET_VISIBLE (sheet->hover_window->window))
5179 if ( sheet->motion_timer > 0 )
5180 g_source_remove (sheet->motion_timer);
5181 sheet->motion_timer = g_timeout_add (TIMEOUT_HOVER, motion_timeout_callback, sheet);
5187 gtk_widget_get_pointer (widget, &wx, &wy);
5189 if ( gtk_sheet_get_pixel_info (sheet, wx, wy, &row, &column) )
5191 if ( row != sheet->hover_window->row || column != sheet->hover_window->column)
5193 gtk_widget_hide (sheet->hover_window->window);
5198 if (event->window == sheet->column_title_window &&
5199 gtk_sheet_columns_resizable (sheet))
5201 gtk_widget_get_pointer (widget, &x, &y);
5202 if (!GTK_SHEET_IN_SELECTION (sheet) &&
5203 POSSIBLE_XDRAG (sheet, x, &column))
5205 new_cursor = GDK_SB_H_DOUBLE_ARROW;
5206 if (new_cursor != sheet->cursor_drag->type)
5208 gdk_cursor_destroy (sheet->cursor_drag);
5209 sheet->cursor_drag = gdk_cursor_new (GDK_SB_H_DOUBLE_ARROW);
5210 gdk_window_set_cursor (sheet->column_title_window,
5211 sheet->cursor_drag);
5216 new_cursor = GDK_TOP_LEFT_ARROW;
5217 if (!GTK_SHEET_IN_XDRAG (sheet) &&
5218 new_cursor != sheet->cursor_drag->type)
5220 gdk_cursor_destroy (sheet->cursor_drag);
5221 sheet->cursor_drag = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
5222 gdk_window_set_cursor (sheet->column_title_window,
5223 sheet->cursor_drag);
5228 if (event->window == sheet->row_title_window &&
5229 gtk_sheet_rows_resizable (sheet))
5231 gtk_widget_get_pointer (widget, &x, &y);
5232 if (!GTK_SHEET_IN_SELECTION (sheet) && POSSIBLE_YDRAG (sheet, y, &column))
5234 new_cursor = GDK_SB_V_DOUBLE_ARROW;
5235 if (new_cursor != sheet->cursor_drag->type)
5237 gdk_cursor_destroy (sheet->cursor_drag);
5238 sheet->cursor_drag = gdk_cursor_new (GDK_SB_V_DOUBLE_ARROW);
5239 gdk_window_set_cursor (sheet->row_title_window, sheet->cursor_drag);
5244 new_cursor = GDK_TOP_LEFT_ARROW;
5245 if (!GTK_SHEET_IN_YDRAG (sheet) &&
5246 new_cursor != sheet->cursor_drag->type)
5248 gdk_cursor_destroy (sheet->cursor_drag);
5249 sheet->cursor_drag = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
5250 gdk_window_set_cursor (sheet->row_title_window, sheet->cursor_drag);
5255 new_cursor = GDK_PLUS;
5256 if ( event->window == sheet->sheet_window &&
5257 !POSSIBLE_DRAG (sheet, x, y, &row, &column) &&
5258 !GTK_SHEET_IN_DRAG (sheet) &&
5259 !POSSIBLE_RESIZE (sheet, x, y, &row, &column) &&
5260 !GTK_SHEET_IN_RESIZE (sheet) &&
5261 new_cursor != sheet->cursor_drag->type)
5263 gdk_cursor_destroy (sheet->cursor_drag);
5264 sheet->cursor_drag = gdk_cursor_new (GDK_PLUS);
5265 gdk_window_set_cursor (sheet->sheet_window, sheet->cursor_drag);
5268 new_cursor = GDK_TOP_LEFT_ARROW;
5269 if ( event->window == sheet->sheet_window &&
5270 ! (POSSIBLE_RESIZE (sheet, x, y, &row, &column) || GTK_SHEET_IN_RESIZE (sheet)) && (POSSIBLE_DRAG (sheet, x, y, &row, &column) || GTK_SHEET_IN_DRAG (sheet)) &&
5272 new_cursor != sheet->cursor_drag->type)
5274 gdk_cursor_destroy (sheet->cursor_drag);
5275 sheet->cursor_drag = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
5276 gdk_window_set_cursor (sheet->sheet_window, sheet->cursor_drag);
5279 new_cursor = GDK_SIZING;
5280 if ( event->window == sheet->sheet_window &&
5281 sheet->selection_mode != GTK_SELECTION_NONE &&
5282 !GTK_SHEET_IN_DRAG (sheet) &&
5283 (POSSIBLE_RESIZE (sheet, x, y, &row, &column) ||
5284 GTK_SHEET_IN_RESIZE (sheet)) &&
5285 new_cursor != sheet->cursor_drag->type)
5287 gdk_cursor_destroy (sheet->cursor_drag);
5288 sheet->cursor_drag = gdk_cursor_new (GDK_SIZING);
5289 gdk_window_set_cursor (sheet->sheet_window, sheet->cursor_drag);
5293 gdk_window_get_pointer (widget->window, &x, &y, &mods);
5294 if (! (mods & GDK_BUTTON1_MASK)) return FALSE;
5296 if (GTK_SHEET_IN_XDRAG (sheet))
5298 if (event->is_hint || event->window != widget->window)
5299 gtk_widget_get_pointer (widget, &x, NULL);
5303 new_column_width (sheet, sheet->drag_cell.col, &x);
5304 if (x != sheet->x_drag)
5306 draw_xor_vline (sheet);
5308 draw_xor_vline (sheet);
5313 if (GTK_SHEET_IN_YDRAG (sheet))
5315 if (event->is_hint || event->window != widget->window)
5316 gtk_widget_get_pointer (widget, NULL, &y);
5320 new_row_height (sheet, sheet->drag_cell.row, &y);
5321 if (y != sheet->y_drag)
5323 draw_xor_hline (sheet);
5325 draw_xor_hline (sheet);
5330 if (GTK_SHEET_IN_DRAG (sheet))
5333 column = COLUMN_FROM_XPIXEL (sheet, x)- sheet->drag_cell.col;
5334 row = ROW_FROM_YPIXEL (sheet, y)- sheet->drag_cell.row;
5335 if (sheet->state == GTK_SHEET_COLUMN_SELECTED) row = 0;
5336 if (sheet->state == GTK_SHEET_ROW_SELECTED) column = 0;
5340 if (aux.row0 + row >= 0 && aux.rowi + row < yyy_row_count (sheet) &&
5341 aux.col0 + column >= 0 && aux.coli + column < xxx_column_count (sheet))
5343 aux = sheet->drag_range;
5344 sheet->drag_range.row0 = sheet->range.row0 + row;
5345 sheet->drag_range.col0 = sheet->range.col0 + column;
5346 sheet->drag_range.rowi = sheet->range.rowi + row;
5347 sheet->drag_range.coli = sheet->range.coli + column;
5348 if (aux.row0 != sheet->drag_range.row0 ||
5349 aux.col0 != sheet->drag_range.col0)
5351 draw_xor_rectangle (sheet, aux);
5352 draw_xor_rectangle (sheet, sheet->drag_range);
5358 if (GTK_SHEET_IN_RESIZE (sheet))
5361 gint v_h, current_col, current_row, col_threshold, row_threshold;
5364 if (abs (x - COLUMN_LEFT_XPIXEL (sheet, sheet->drag_cell.col)) >
5365 abs (y - ROW_TOP_YPIXEL (sheet, sheet->drag_cell.row))) v_h = 2;
5367 current_col = COLUMN_FROM_XPIXEL (sheet, x);
5368 current_row = ROW_FROM_YPIXEL (sheet, y);
5369 column = current_col - sheet->drag_cell.col;
5370 row = current_row - sheet->drag_cell.row;
5372 /*use half of column width resp. row height as threshold to
5374 col_threshold = COLUMN_LEFT_XPIXEL (sheet, current_col) +
5375 xxx_column_width (sheet, current_col) / 2;
5378 if (x < col_threshold)
5381 else if (column < 0)
5383 if (x > col_threshold)
5386 row_threshold = ROW_TOP_YPIXEL (sheet, current_row) +
5387 yyy_row_height (sheet, current_row)/2;
5390 if (y < row_threshold)
5395 if (y > row_threshold)
5399 if (sheet->state == GTK_SHEET_COLUMN_SELECTED) row = 0;
5400 if (sheet->state == GTK_SHEET_ROW_SELECTED) column = 0;
5410 if (aux.row0 + row >= 0 && aux.rowi + row < yyy_row_count (sheet) &&
5411 aux.col0 + column >= 0 && aux.coli + column < xxx_column_count (sheet))
5413 aux = sheet->drag_range;
5414 sheet->drag_range = sheet->range;
5416 if (row < 0) sheet->drag_range.row0 = sheet->range.row0 + row;
5417 if (row > 0) sheet->drag_range.rowi = sheet->range.rowi + row;
5418 if (column < 0) sheet->drag_range.col0 = sheet->range.col0 + column;
5419 if (column > 0) sheet->drag_range.coli = sheet->range.coli + column;
5421 if (aux.row0 != sheet->drag_range.row0 ||
5422 aux.rowi != sheet->drag_range.rowi ||
5423 aux.col0 != sheet->drag_range.col0 ||
5424 aux.coli != sheet->drag_range.coli)
5426 draw_xor_rectangle (sheet, aux);
5427 draw_xor_rectangle (sheet, sheet->drag_range);
5433 gtk_sheet_get_pixel_info (sheet, x, y, &row, &column);
5435 if (sheet->state == GTK_SHEET_NORMAL && row == sheet->active_cell.row &&
5436 column == sheet->active_cell.col) return TRUE;
5438 if (GTK_SHEET_IN_SELECTION (sheet) && mods&GDK_BUTTON1_MASK)
5439 gtk_sheet_extend_selection (sheet, row, column);
5445 gtk_sheet_move_query (GtkSheet *sheet, gint row, gint column)
5447 gint row_move, column_move;
5448 gfloat row_align, col_align;
5449 guint height, width;
5451 gint new_col = column;
5454 column_move = FALSE;
5458 height = sheet->sheet_window_height;
5459 width = sheet->sheet_window_width;
5461 if (row >= MAX_VISIBLE_ROW (sheet) && sheet->state != GTK_SHEET_COLUMN_SELECTED)
5464 new_row = MIN (yyy_row_count (sheet) - 1, row + 1);
5466 if (MAX_VISIBLE_ROW (sheet) == yyy_row_count (sheet) - 1 &&
5467 ROW_TOP_YPIXEL (sheet, yyy_row_count (sheet)- 1) +
5468 yyy_row_height (sheet, yyy_row_count (sheet)- 1) < height)
5474 if (row < MIN_VISIBLE_ROW (sheet) && sheet->state != GTK_SHEET_COLUMN_SELECTED)
5479 if (column >= MAX_VISIBLE_COLUMN (sheet) && sheet->state != GTK_SHEET_ROW_SELECTED)
5482 new_col = MIN (xxx_column_count (sheet) - 1, column + 1);
5484 if (MAX_VISIBLE_COLUMN (sheet) == (xxx_column_count (sheet) - 1) &&
5485 COLUMN_LEFT_XPIXEL (sheet, xxx_column_count (sheet) - 1) +
5486 xxx_column_width (sheet, xxx_column_count (sheet) - 1) < width)
5488 column_move = FALSE;
5492 if (column < MIN_VISIBLE_COLUMN (sheet) && sheet->state != GTK_SHEET_ROW_SELECTED)
5498 if (row_move || column_move)
5500 gtk_sheet_moveto (sheet, new_row, new_col, row_align, col_align);
5503 return (row_move || column_move);
5507 gtk_sheet_extend_selection (GtkSheet *sheet, gint row, gint column)
5509 GtkSheetRange range;
5513 if (row == sheet->selection_cell.row && column == sheet->selection_cell.col)
5516 if (sheet->selection_mode == GTK_SELECTION_SINGLE) return;
5518 gtk_sheet_move_query (sheet, row, column);
5519 gtk_widget_grab_focus (GTK_WIDGET (sheet));
5521 if (GTK_SHEET_IN_DRAG (sheet)) return;
5523 state = sheet->state;
5525 switch (sheet->state)
5527 case GTK_SHEET_ROW_SELECTED:
5528 column = xxx_column_count (sheet) - 1;
5530 case GTK_SHEET_COLUMN_SELECTED:
5531 row = yyy_row_count (sheet) - 1;
5533 case GTK_SHEET_NORMAL:
5534 sheet->state = GTK_SHEET_RANGE_SELECTED;
5535 r = sheet->active_cell.row;
5536 c = sheet->active_cell.col;
5537 sheet->range.col0 = c;
5538 sheet->range.row0 = r;
5539 sheet->range.coli = c;
5540 sheet->range.rowi = r;
5541 gdk_draw_pixmap (sheet->sheet_window,
5542 GTK_WIDGET (sheet)->style->fg_gc[GTK_STATE_NORMAL],
5544 COLUMN_LEFT_XPIXEL (sheet, c)- 1,
5545 ROW_TOP_YPIXEL (sheet, r)- 1,
5546 COLUMN_LEFT_XPIXEL (sheet, c)- 1,
5547 ROW_TOP_YPIXEL (sheet, r)- 1,
5548 xxx_column_width (sheet, c)+4,
5549 yyy_row_height (sheet, r)+4);
5550 gtk_sheet_range_draw_selection (sheet, sheet->range);
5551 case GTK_SHEET_RANGE_SELECTED:
5552 sheet->state = GTK_SHEET_RANGE_SELECTED;
5555 sheet->selection_cell.row = row;
5556 sheet->selection_cell.col = column;
5558 range.col0 = MIN (column, sheet->active_cell.col);
5559 range.coli = MAX (column, sheet->active_cell.col);
5560 range.row0 = MIN (row, sheet->active_cell.row);
5561 range.rowi = MAX (row, sheet->active_cell.row);
5563 if (range.row0 != sheet->range.row0 || range.rowi != sheet->range.rowi ||
5564 range.col0 != sheet->range.col0 || range.coli != sheet->range.coli ||
5565 state == GTK_SHEET_NORMAL)
5566 gtk_sheet_real_select_range (sheet, &range);
5571 gtk_sheet_entry_key_press (GtkWidget *widget,
5575 g_signal_emit_by_name (widget, "key_press_event", key, &focus);
5580 gtk_sheet_key_press (GtkWidget *widget,
5586 gboolean extend_selection = FALSE;
5587 gboolean force_move = FALSE;
5588 gboolean in_selection = FALSE;
5589 gboolean veto = TRUE;
5592 sheet = GTK_SHEET (widget);
5594 if (key->state & GDK_CONTROL_MASK || key->keyval == GDK_Control_L ||
5595 key->keyval == GDK_Control_R) return FALSE;
5597 extend_selection = (key->state & GDK_SHIFT_MASK) || key->keyval == GDK_Shift_L
5598 || key->keyval == GDK_Shift_R;
5600 state = sheet->state;
5601 in_selection = GTK_SHEET_IN_SELECTION (sheet);
5602 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
5604 switch (key->keyval)
5606 case GDK_Return: case GDK_KP_Enter:
5607 if (sheet->state == GTK_SHEET_NORMAL &&
5608 !GTK_SHEET_IN_SELECTION (sheet))
5609 g_signal_stop_emission_by_name (gtk_sheet_get_entry (sheet),
5611 row = sheet->active_cell.row;
5612 col = sheet->active_cell.col;
5613 if (sheet->state == GTK_SHEET_COLUMN_SELECTED)
5614 row = MIN_VISIBLE_ROW (sheet)- 1;
5615 if (sheet->state == GTK_SHEET_ROW_SELECTED)
5616 col = MIN_VISIBLE_COLUMN (sheet);
5617 if (row < yyy_row_count (sheet) - 1)
5620 while (!yyy_row_is_visible (sheet, row) && row < yyy_row_count (sheet)- 1)
5623 gtk_sheet_click_cell (sheet, row, col, &veto);
5624 extend_selection = FALSE;
5626 case GDK_ISO_Left_Tab:
5627 row = sheet->active_cell.row;
5628 col = sheet->active_cell.col;
5629 if (sheet->state == GTK_SHEET_ROW_SELECTED)
5630 col = MIN_VISIBLE_COLUMN (sheet)- 1;
5631 if (sheet->state == GTK_SHEET_COLUMN_SELECTED)
5632 row = MIN_VISIBLE_ROW (sheet);
5636 while (! xxx_column_is_visible (sheet, col) && col > 0) col--;
5639 gtk_sheet_click_cell (sheet, row, col, &veto);
5640 extend_selection = FALSE;
5643 row = sheet->active_cell.row;
5644 col = sheet->active_cell.col;
5645 if (sheet->state == GTK_SHEET_ROW_SELECTED)
5646 col = MIN_VISIBLE_COLUMN (sheet)- 1;
5647 if (sheet->state == GTK_SHEET_COLUMN_SELECTED)
5648 row = MIN_VISIBLE_ROW (sheet);
5649 if (col < xxx_column_count (sheet) - 1)
5652 while (! xxx_column_is_visible (sheet, col) &&
5653 col < xxx_column_count (sheet) - 1)
5656 gtk_sheet_click_cell (sheet, row, col, &veto);
5657 extend_selection = FALSE;
5660 scroll = MAX_VISIBLE_ROW (sheet)- MIN_VISIBLE_ROW (sheet)+1;
5662 if (extend_selection)
5664 if (state == GTK_STATE_NORMAL)
5666 row = sheet->active_cell.row;
5667 col = sheet->active_cell.col;
5668 gtk_sheet_click_cell (sheet, row, col, &veto);
5671 if (sheet->selection_cell.row > 0)
5673 row = sheet->selection_cell.row - scroll;
5674 while (!yyy_row_is_visible (sheet, row) && row > 0) row--;
5676 gtk_sheet_extend_selection (sheet, row, sheet->selection_cell.col);
5680 col = sheet->active_cell.col;
5681 row = sheet->active_cell.row;
5682 if (state == GTK_SHEET_COLUMN_SELECTED)
5683 row = MIN_VISIBLE_ROW (sheet);
5684 if (state == GTK_SHEET_ROW_SELECTED)
5685 col = MIN_VISIBLE_COLUMN (sheet);
5687 while (!yyy_row_is_visible (sheet, row) && row > 0) row--;
5689 gtk_sheet_click_cell (sheet, row, col, &veto);
5690 extend_selection = FALSE;
5693 scroll = MAX_VISIBLE_ROW (sheet)- MIN_VISIBLE_ROW (sheet)+1;
5695 if (extend_selection)
5697 if (state == GTK_STATE_NORMAL)
5699 row = sheet->active_cell.row;
5700 col = sheet->active_cell.col;
5701 gtk_sheet_click_cell (sheet, row, col, &veto);
5704 if (sheet->selection_cell.row < yyy_row_count (sheet)- 1)
5706 row = sheet->selection_cell.row + scroll;
5707 while (!yyy_row_is_visible (sheet, row) && row < yyy_row_count (sheet)- 1) row++;
5708 row = MIN (yyy_row_count (sheet)- 1, row);
5709 gtk_sheet_extend_selection (sheet, row, sheet->selection_cell.col);
5713 col = sheet->active_cell.col;
5714 row = sheet->active_cell.row;
5715 if (sheet->active_cell.row < yyy_row_count (sheet)- 1)
5717 if (state == GTK_SHEET_COLUMN_SELECTED)
5718 row = MIN_VISIBLE_ROW (sheet)- 1;
5719 if (state == GTK_SHEET_ROW_SELECTED)
5720 col = MIN_VISIBLE_COLUMN (sheet);
5722 while (!yyy_row_is_visible (sheet, row) && row < yyy_row_count (sheet)- 1) row++;
5723 row = MIN (yyy_row_count (sheet)- 1, row);
5725 gtk_sheet_click_cell (sheet, row, col, &veto);
5726 extend_selection = FALSE;
5729 if (extend_selection)
5731 if (state == GTK_STATE_NORMAL)
5733 row = sheet->active_cell.row;
5734 col = sheet->active_cell.col;
5735 gtk_sheet_click_cell (sheet, row, col, &veto);
5738 if (sheet->selection_cell.col < xxx_column_count (sheet) - 1)
5740 col = sheet->selection_cell.col + 1;
5741 while (! xxx_column_is_visible (sheet, col) && col < xxx_column_count (sheet) - 1)
5743 gtk_sheet_extend_selection (sheet, sheet->selection_cell.row, col);
5747 col = sheet->active_cell.col;
5748 row = sheet->active_cell.row;
5749 if (sheet->active_cell.col < xxx_column_count (sheet) - 1)
5752 if (state == GTK_SHEET_ROW_SELECTED)
5753 col = MIN_VISIBLE_COLUMN (sheet)- 1;
5754 if (state == GTK_SHEET_COLUMN_SELECTED)
5755 row = MIN_VISIBLE_ROW (sheet);
5756 while (! xxx_column_is_visible (sheet, col) && col < xxx_column_count (sheet) - 1) col++;
5757 if (strlen (gtk_entry_get_text (GTK_ENTRY (gtk_sheet_get_entry (sheet)))) == 0
5760 gtk_sheet_click_cell (sheet, row, col, &veto);
5765 extend_selection = FALSE;
5768 if (extend_selection)
5770 if (state == GTK_STATE_NORMAL)
5772 row = sheet->active_cell.row;
5773 col = sheet->active_cell.col;
5774 gtk_sheet_click_cell (sheet, row, col, &veto);
5777 if (sheet->selection_cell.col > 0)
5779 col = sheet->selection_cell.col - 1;
5780 while (! xxx_column_is_visible (sheet, col) && col > 0) col--;
5781 gtk_sheet_extend_selection (sheet, sheet->selection_cell.row, col);
5785 col = sheet->active_cell.col - 1;
5786 row = sheet->active_cell.row;
5787 if (state == GTK_SHEET_ROW_SELECTED)
5788 col = MIN_VISIBLE_COLUMN (sheet)- 1;
5789 if (state == GTK_SHEET_COLUMN_SELECTED)
5790 row = MIN_VISIBLE_ROW (sheet);
5791 while (! xxx_column_is_visible (sheet, col) && col > 0) col--;
5794 if (strlen (gtk_entry_get_text (GTK_ENTRY (gtk_sheet_get_entry (sheet)))) == 0
5797 gtk_sheet_click_cell (sheet, row, col, &veto);
5801 extend_selection = FALSE;
5805 while (!yyy_row_is_visible (sheet, row) && row < yyy_row_count (sheet) - 1) row++;
5806 gtk_sheet_click_cell (sheet, row, sheet->active_cell.col, &veto);
5807 extend_selection = FALSE;
5810 row = yyy_row_count (sheet) - 1;
5811 while (!yyy_row_is_visible (sheet, row) && row > 0) row--;
5812 gtk_sheet_click_cell (sheet, row, sheet->active_cell.col, &veto);
5813 extend_selection = FALSE;
5818 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
5819 if (extend_selection) return TRUE;
5821 if (state == GTK_SHEET_ROW_SELECTED)
5822 sheet->active_cell.col = MIN_VISIBLE_COLUMN (sheet);
5823 if (state == GTK_SHEET_COLUMN_SELECTED)
5824 sheet->active_cell.row = MIN_VISIBLE_ROW (sheet);
5828 if (extend_selection) return TRUE;
5830 gtk_sheet_activate_cell (sheet, sheet->active_cell.row,
5831 sheet->active_cell.col);
5837 gtk_sheet_size_request (GtkWidget * widget,
5838 GtkRequisition * requisition)
5842 GtkSheetChild *child;
5843 GtkRequisition child_requisition;
5845 g_return_if_fail (widget != NULL);
5846 g_return_if_fail (GTK_IS_SHEET (widget));
5847 g_return_if_fail (requisition != NULL);
5849 sheet = GTK_SHEET (widget);
5851 requisition->width = 3*DEFAULT_COLUMN_WIDTH;
5852 requisition->height = 3*DEFAULT_ROW_HEIGHT (widget);
5854 /* compute the size of the column title area */
5855 if (sheet->column_titles_visible)
5856 requisition->height += sheet->column_title_area.height;
5858 /* compute the size of the row title area */
5859 if (sheet->row_titles_visible)
5860 requisition->width += sheet->row_title_area.width;
5862 children = sheet->children;
5865 child = children->data;
5866 children = children->next;
5868 gtk_widget_size_request (child->widget, &child_requisition);
5874 gtk_sheet_size_allocate (GtkWidget * widget,
5875 GtkAllocation * allocation)
5878 GtkAllocation sheet_allocation;
5881 g_return_if_fail (widget != NULL);
5882 g_return_if_fail (GTK_IS_SHEET (widget));
5883 g_return_if_fail (allocation != NULL);
5885 sheet = GTK_SHEET (widget);
5886 widget->allocation = *allocation;
5887 border_width = GTK_CONTAINER (widget)->border_width;
5889 if (GTK_WIDGET_REALIZED (widget))
5890 gdk_window_move_resize (widget->window,
5891 allocation->x + border_width,
5892 allocation->y + border_width,
5893 allocation->width - 2 * border_width,
5894 allocation->height - 2 * border_width);
5896 /* use internal allocation structure for all the math
5897 * because it's easier than always subtracting the container
5899 sheet->internal_allocation.x = 0;
5900 sheet->internal_allocation.y = 0;
5901 sheet->internal_allocation.width = allocation->width - 2 * border_width;
5902 sheet->internal_allocation.height = allocation->height - 2 * border_width;
5904 sheet_allocation.x = 0;
5905 sheet_allocation.y = 0;
5906 sheet_allocation.width = allocation->width - 2 * border_width;
5907 sheet_allocation.height = allocation->height - 2 * border_width;
5909 sheet->sheet_window_width = sheet_allocation.width;
5910 sheet->sheet_window_height = sheet_allocation.height;
5912 if (GTK_WIDGET_REALIZED (widget))
5913 gdk_window_move_resize (sheet->sheet_window,
5916 sheet_allocation.width,
5917 sheet_allocation.height);
5919 /* position the window which holds the column title buttons */
5920 sheet->column_title_area.x = 0;
5921 sheet->column_title_area.y = 0;
5922 if (sheet->row_titles_visible)
5923 sheet->column_title_area.x = sheet->row_title_area.width;
5924 sheet->column_title_area.width = sheet_allocation.width -
5925 sheet->column_title_area.x;
5926 if (GTK_WIDGET_REALIZED (widget) && sheet->column_titles_visible)
5927 gdk_window_move_resize (sheet->column_title_window,
5928 sheet->column_title_area.x,
5929 sheet->column_title_area.y,
5930 sheet->column_title_area.width,
5931 sheet->column_title_area.height);
5933 sheet->sheet_window_width = sheet_allocation.width;
5934 sheet->sheet_window_height = sheet_allocation.height;
5936 /* column button allocation */
5937 size_allocate_column_title_buttons (sheet);
5939 /* position the window which holds the row title buttons */
5940 sheet->row_title_area.x = 0;
5941 sheet->row_title_area.y = 0;
5942 if (sheet->column_titles_visible)
5943 sheet->row_title_area.y = sheet->column_title_area.height;
5944 sheet->row_title_area.height = sheet_allocation.height -
5945 sheet->row_title_area.y;
5947 if (GTK_WIDGET_REALIZED (widget) && sheet->row_titles_visible)
5948 gdk_window_move_resize (sheet->row_title_window,
5949 sheet->row_title_area.x,
5950 sheet->row_title_area.y,
5951 sheet->row_title_area.width,
5952 sheet->row_title_area.height);
5955 /* row button allocation */
5956 size_allocate_row_title_buttons (sheet);
5957 size_allocate_column_title_buttons (sheet);
5959 /* re - scale backing pixmap */
5960 gtk_sheet_make_backing_pixmap (sheet, 0, 0);
5961 gtk_sheet_position_children (sheet);
5963 /* set the scrollbars adjustments */
5964 adjust_scrollbars (sheet);
5968 size_allocate_column_title_buttons (GtkSheet * sheet)
5973 if (!sheet->column_titles_visible) return;
5974 if (!GTK_WIDGET_REALIZED (sheet))
5977 width = sheet->sheet_window_width;
5980 if (sheet->row_titles_visible)
5982 width -= sheet->row_title_area.width;
5983 x = sheet->row_title_area.width;
5986 if (sheet->column_title_area.width != width || sheet->column_title_area.x != x)
5988 sheet->column_title_area.width = width;
5989 sheet->column_title_area.x = x;
5990 gdk_window_move_resize (sheet->column_title_window,
5991 sheet->column_title_area.x,
5992 sheet->column_title_area.y,
5993 sheet->column_title_area.width,
5994 sheet->column_title_area.height);
5998 if (MAX_VISIBLE_COLUMN (sheet) == xxx_column_count (sheet) - 1)
5999 gdk_window_clear_area (sheet->column_title_window,
6001 sheet->column_title_area.width,
6002 sheet->column_title_area.height);
6004 if (!GTK_WIDGET_DRAWABLE (sheet)) return;
6006 size_allocate_global_button (sheet);
6008 for (i = MIN_VISIBLE_COLUMN (sheet); i <= MAX_VISIBLE_COLUMN (sheet); i++)
6009 gtk_sheet_column_title_button_draw (sheet, i);
6013 size_allocate_row_title_buttons (GtkSheet * sheet)
6018 if (!sheet->row_titles_visible) return;
6019 if (!GTK_WIDGET_REALIZED (sheet))
6022 height = sheet->sheet_window_height;
6025 if (sheet->column_titles_visible)
6027 height -= sheet->column_title_area.height;
6028 y = sheet->column_title_area.height;
6031 if (sheet->row_title_area.height != height || sheet->row_title_area.y != y)
6033 sheet->row_title_area.y = y;
6034 sheet->row_title_area.height = height;
6035 gdk_window_move_resize (sheet->row_title_window,
6036 sheet->row_title_area.x,
6037 sheet->row_title_area.y,
6038 sheet->row_title_area.width,
6039 sheet->row_title_area.height);
6041 if (MAX_VISIBLE_ROW (sheet) == yyy_row_count (sheet)- 1)
6042 gdk_window_clear_area (sheet->row_title_window,
6044 sheet->row_title_area.width,
6045 sheet->row_title_area.height);
6047 if (!GTK_WIDGET_DRAWABLE (sheet)) return;
6049 size_allocate_global_button (sheet);
6051 for (i = MIN_VISIBLE_ROW (sheet); i <= MAX_VISIBLE_ROW (sheet); i++)
6053 if ( i >= yyy_row_count (sheet))
6055 gtk_sheet_row_title_button_draw (sheet, i);
6061 gtk_sheet_size_allocate_entry (GtkSheet *sheet)
6063 GtkAllocation shentry_allocation;
6064 GtkSheetCellAttr attributes = { 0 };
6065 GtkEntry *sheet_entry;
6066 GtkStyle *style = NULL, *previous_style = NULL;
6068 gint size, max_size, text_size, column_width;
6071 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
6072 if (!GTK_WIDGET_MAPPED (GTK_WIDGET (sheet))) return;
6074 sheet_entry = GTK_ENTRY (gtk_sheet_get_entry (sheet));
6076 if ( ! gtk_sheet_get_attributes (sheet, sheet->active_cell.row,
6077 sheet->active_cell.col,
6081 if ( GTK_WIDGET_REALIZED (sheet->entry_widget) )
6083 if (!GTK_WIDGET (sheet_entry)->style)
6084 gtk_widget_ensure_style (GTK_WIDGET (sheet_entry));
6086 previous_style = GTK_WIDGET (sheet_entry)->style;
6088 style = gtk_style_copy (previous_style);
6089 style->bg[GTK_STATE_NORMAL] = attributes.background;
6090 style->fg[GTK_STATE_NORMAL] = attributes.foreground;
6091 style->text[GTK_STATE_NORMAL] = attributes.foreground;
6092 style->bg[GTK_STATE_ACTIVE] = attributes.background;
6093 style->fg[GTK_STATE_ACTIVE] = attributes.foreground;
6094 style->text[GTK_STATE_ACTIVE] = attributes.foreground;
6096 pango_font_description_free (style->font_desc);
6097 g_assert (attributes.font_desc);
6098 style->font_desc = pango_font_description_copy (attributes.font_desc);
6100 GTK_WIDGET (sheet_entry)->style = style;
6101 gtk_widget_size_request (sheet->entry_widget, NULL);
6102 GTK_WIDGET (sheet_entry)->style = previous_style;
6104 if (style != previous_style)
6106 if (!GTK_IS_ITEM_ENTRY (sheet->entry_widget))
6108 style->bg[GTK_STATE_NORMAL] = previous_style->bg[GTK_STATE_NORMAL];
6109 style->fg[GTK_STATE_NORMAL] = previous_style->fg[GTK_STATE_NORMAL];
6110 style->bg[GTK_STATE_ACTIVE] = previous_style->bg[GTK_STATE_ACTIVE];
6111 style->fg[GTK_STATE_ACTIVE] = previous_style->fg[GTK_STATE_ACTIVE];
6113 gtk_widget_set_style (GTK_WIDGET (sheet_entry), style);
6114 g_object_unref (style);
6118 if (GTK_IS_ITEM_ENTRY (sheet_entry))
6119 max_size = GTK_ITEM_ENTRY (sheet_entry)->text_max_size;
6124 text = gtk_entry_get_text (GTK_ENTRY (sheet_entry));
6125 if (text && strlen (text) > 0)
6126 text_size = STRING_WIDTH (GTK_WIDGET (sheet), attributes.font_desc, text);
6128 column_width = xxx_column_width (sheet, sheet->active_cell.col);
6130 size = MIN (text_size, max_size);
6131 size = MAX (size, column_width - 2 * CELLOFFSET);
6133 row = sheet->active_cell.row;
6134 col = sheet->active_cell.col;
6136 shentry_allocation.x = COLUMN_LEFT_XPIXEL (sheet, sheet->active_cell.col);
6137 shentry_allocation.y = ROW_TOP_YPIXEL (sheet, sheet->active_cell.row);
6138 shentry_allocation.width = column_width;
6139 shentry_allocation.height = yyy_row_height (sheet, sheet->active_cell.row);
6141 if (GTK_IS_ITEM_ENTRY (sheet->entry_widget))
6143 shentry_allocation.height -= 2 * CELLOFFSET;
6144 shentry_allocation.y += CELLOFFSET;
6145 shentry_allocation.width = size;
6147 switch (GTK_ITEM_ENTRY (sheet_entry)->justification)
6149 case GTK_JUSTIFY_CENTER:
6150 shentry_allocation.x += column_width / 2 - size / 2;
6152 case GTK_JUSTIFY_RIGHT:
6153 shentry_allocation.x += column_width - size - CELLOFFSET;
6155 case GTK_JUSTIFY_LEFT:
6156 case GTK_JUSTIFY_FILL:
6157 shentry_allocation.x += CELLOFFSET;
6162 if (!GTK_IS_ITEM_ENTRY (sheet->entry_widget))
6164 shentry_allocation.x += 2;
6165 shentry_allocation.y += 2;
6166 shentry_allocation.width -= MIN (shentry_allocation.width, 3);
6167 shentry_allocation.height -= MIN (shentry_allocation.height, 3);
6170 gtk_widget_size_allocate (sheet->entry_widget, &shentry_allocation);
6172 if (previous_style == style) g_object_unref (previous_style);
6176 gtk_sheet_entry_set_max_size (GtkSheet *sheet)
6180 gint sizel = 0, sizer = 0;
6182 GtkJustification justification;
6185 row = sheet->active_cell.row;
6186 col = sheet->active_cell.col;
6188 if ( ! GTK_IS_ITEM_ENTRY (sheet->entry_widget) )
6191 justification = GTK_ITEM_ENTRY (sheet->entry_widget)->justification;
6193 switch (justification)
6195 case GTK_JUSTIFY_FILL:
6196 case GTK_JUSTIFY_LEFT:
6197 for (i = col + 1; i <= MAX_VISIBLE_COLUMN (sheet); i++)
6199 if ((s = gtk_sheet_cell_get_text (sheet, row, i)))
6204 size +=xxx_column_width (sheet, i);
6206 size = MIN (size, sheet->sheet_window_width - COLUMN_LEFT_XPIXEL (sheet, col));
6208 case GTK_JUSTIFY_RIGHT:
6209 for (i = col - 1; i >= MIN_VISIBLE_COLUMN (sheet); i--)
6211 if ((s = gtk_sheet_cell_get_text (sheet, row, i)))
6216 size +=xxx_column_width (sheet, i);
6219 case GTK_JUSTIFY_CENTER:
6220 for (i = col + 1; i <= MAX_VISIBLE_COLUMN (sheet); i++)
6222 sizer += xxx_column_width (sheet, i);
6224 for (i = col - 1; i >= MIN_VISIBLE_COLUMN (sheet); i--)
6226 if ((s = gtk_sheet_cell_get_text (sheet, row, i)))
6231 sizel +=xxx_column_width (sheet, i);
6233 size = 2 * MIN (sizel, sizer);
6238 size += xxx_column_width (sheet, col);
6239 GTK_ITEM_ENTRY (sheet->entry_widget)->text_max_size = size;
6244 create_sheet_entry (GtkSheet *sheet)
6246 if (sheet->entry_widget)
6248 gtk_widget_unparent (sheet->entry_widget);
6251 if (sheet->entry_type)
6253 sheet->entry_container = g_object_new (sheet->entry_type, NULL);
6254 g_object_ref_sink (sheet->entry_container);
6255 sheet->entry_widget = gtk_sheet_get_entry (sheet);
6257 if ( NULL == sheet->entry_widget)
6259 g_warning ("Entry type is %s. It must be GtkEntry subclass, or a widget containing one. "
6260 "Using default", g_type_name (sheet->entry_type));
6261 g_object_unref (sheet->entry_container);
6262 sheet->entry_widget = sheet->entry_container = gtk_item_entry_new ();
6266 sheet->entry_widget = sheet->entry_container ;
6271 sheet->entry_widget = sheet->entry_container = gtk_item_entry_new ();
6272 g_object_ref_sink (sheet->entry_container);
6275 gtk_widget_size_request (sheet->entry_widget, NULL);
6277 if (GTK_WIDGET_REALIZED (sheet))
6279 gtk_widget_set_parent_window (sheet->entry_widget, sheet->sheet_window);
6280 gtk_widget_set_parent (sheet->entry_widget, GTK_WIDGET (sheet));
6281 gtk_widget_realize (sheet->entry_widget);
6284 g_signal_connect_swapped (sheet->entry_widget, "key_press_event",
6285 G_CALLBACK (gtk_sheet_entry_key_press),
6288 gtk_widget_show (sheet->entry_widget);
6292 /* Finds the last child widget that happens to be of type GtkEntry */
6294 find_entry (GtkWidget *w, gpointer user_data)
6296 GtkWidget **entry = user_data;
6297 if ( GTK_IS_ENTRY (w))
6304 gtk_sheet_get_entry (GtkSheet *sheet)
6307 GtkWidget *entry = NULL;
6308 GtkTableChild *table_child;
6309 GtkBoxChild *box_child;
6310 GList *children = NULL;
6312 g_return_val_if_fail (sheet != NULL, NULL);
6313 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
6314 g_return_val_if_fail (sheet->entry_widget != NULL, NULL);
6316 if (GTK_IS_ENTRY (sheet->entry_container))
6317 return (sheet->entry_container);
6319 //parent = GTK_WIDGET (sheet->entry_widget);
6320 parent = sheet->entry_container;
6322 if (GTK_IS_TABLE (parent)) children = GTK_TABLE (parent)->children;
6323 if (GTK_IS_BOX (parent)) children = GTK_BOX (parent)->children;
6325 if (GTK_IS_CONTAINER (parent))
6327 gtk_container_forall (GTK_CONTAINER (parent), find_entry, &entry);
6329 if (GTK_IS_ENTRY (entry))
6333 if (!children) return NULL;
6337 if (GTK_IS_TABLE (parent))
6339 table_child = children->data;
6340 entry = table_child->widget;
6342 if (GTK_IS_BOX (parent))
6344 box_child = children->data;
6345 entry = box_child->widget;
6348 if (GTK_IS_ENTRY (entry))
6350 children = children->next;
6354 if (!GTK_IS_ENTRY (entry)) return NULL;
6361 gtk_sheet_get_entry_widget (GtkSheet *sheet)
6363 g_return_val_if_fail (sheet != NULL, NULL);
6364 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
6365 g_return_val_if_fail (sheet->entry_widget != NULL, NULL);
6367 return (sheet->entry_widget);
6372 gtk_sheet_button_draw (GtkSheet *sheet, GdkWindow *window,
6373 GtkSheetButton *button, gboolean is_sensitive,
6374 GdkRectangle allocation)
6376 GtkShadowType shadow_type;
6377 gint text_width = 0, text_height = 0;
6378 GtkSheetChild *child = NULL;
6379 PangoAlignment align = PANGO_ALIGN_LEFT;
6387 g_return_if_fail (sheet != NULL);
6388 g_return_if_fail (button != NULL);
6390 rtl = gtk_widget_get_direction (GTK_WIDGET (sheet)) == GTK_TEXT_DIR_RTL;
6392 gdk_window_clear_area (window,
6393 allocation.x, allocation.y,
6394 allocation.width, allocation.height);
6396 gtk_paint_box (sheet->button->style, window,
6397 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
6398 &allocation, GTK_WIDGET (sheet->button),
6400 allocation.x, allocation.y,
6401 allocation.width, allocation.height);
6403 state = button->state;
6404 if (!is_sensitive) state = GTK_STATE_INSENSITIVE;
6406 if (state == GTK_STATE_ACTIVE)
6407 shadow_type = GTK_SHADOW_IN;
6409 shadow_type = GTK_SHADOW_OUT;
6411 if (state != GTK_STATE_NORMAL && state != GTK_STATE_INSENSITIVE)
6412 gtk_paint_box (sheet->button->style, window,
6413 button->state, shadow_type,
6414 &allocation, GTK_WIDGET (sheet->button),
6416 allocation.x, allocation.y,
6417 allocation.width, allocation.height);
6419 if (button->label_visible)
6422 text_height = DEFAULT_ROW_HEIGHT (GTK_WIDGET (sheet))- 2 * CELLOFFSET;
6424 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->fg_gc[button->state],
6426 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->white_gc, &allocation);
6428 allocation.y += 2 * sheet->button->style->ythickness;
6431 if (button->label && strlen (button->label)>0)
6434 PangoLayout *layout = NULL;
6435 gint real_x = allocation.x, real_y = allocation.y;
6437 words = button->label;
6438 line = g_new (gchar, 1);
6441 while (words && *words != '\0')
6445 len = strlen (line);
6446 line = g_realloc (line, len + 2);
6450 if (*words == '\n' || * (words + 1) == '\0')
6452 text_width = STRING_WIDTH (GTK_WIDGET (sheet), GTK_WIDGET (sheet)->style->font_desc, line);
6454 layout = gtk_widget_create_pango_layout (GTK_WIDGET (sheet), line);
6455 switch (button->justification)
6457 case GTK_JUSTIFY_LEFT:
6458 real_x = allocation.x + CELLOFFSET;
6459 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
6461 case GTK_JUSTIFY_RIGHT:
6462 real_x = allocation.x + allocation.width - text_width - CELLOFFSET;
6463 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
6465 case GTK_JUSTIFY_CENTER:
6467 real_x = allocation.x + (allocation.width - text_width)/2;
6468 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
6469 pango_layout_set_justify (layout, TRUE);
6471 pango_layout_set_alignment (layout, align);
6472 gtk_paint_layout (GTK_WIDGET (sheet)->style,
6481 g_object_unref (layout);
6483 real_y += text_height + 2;
6486 line = g_new (gchar, 1);
6494 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->fg_gc[button->state],
6496 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->white_gc, NULL);
6500 if ((child = button->child) && (child->widget))
6502 child->x = allocation.x;
6503 child->y = allocation.y;
6505 child->x += (allocation.width - child->widget->requisition.width) / 2;
6506 child->y += (allocation.height - child->widget->requisition.height) / 2;
6507 allocation.x = child->x;
6508 allocation.y = child->y;
6509 allocation.width = child->widget->requisition.width;
6510 allocation.height = child->widget->requisition.height;
6512 allocation.x = child->x;
6513 allocation.y = child->y;
6515 gtk_widget_set_state (child->widget, button->state);
6517 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)) &&
6518 GTK_WIDGET_MAPPED (child->widget))
6520 gtk_widget_size_allocate (child->widget,
6522 gtk_widget_queue_draw (child->widget);
6526 gtk_sheet_button_free (button);
6530 /* COLUMN value of - 1 indicates that the area to the right of the rightmost
6531 button should be redrawn */
6533 gtk_sheet_column_title_button_draw (GtkSheet *sheet, gint column)
6535 GdkWindow *window = NULL;
6536 GdkRectangle allocation;
6538 gboolean is_sensitive = FALSE;
6540 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
6542 if (column >= 0 && ! xxx_column_is_visible (sheet, column)) return;
6543 if (column >= 0 && !sheet->column_titles_visible) return;
6544 if (column >= 0 && column < MIN_VISIBLE_COLUMN (sheet)) return;
6545 if (column >= 0 && column > MAX_VISIBLE_COLUMN (sheet)) return;
6547 window = sheet->column_title_window;
6549 allocation.height = sheet->column_title_area.height;
6553 const gint cols = xxx_column_count (sheet) ;
6554 allocation.x = COLUMN_LEFT_XPIXEL (sheet, cols - 1)
6556 allocation.width = sheet->column_title_area.width
6557 + sheet->column_title_area.x
6560 gdk_window_clear_area (window,
6561 allocation.x, allocation.y,
6562 allocation.width, allocation.height);
6566 GtkSheetButton *button = xxx_column_button (sheet, column);
6567 allocation.x = COLUMN_LEFT_XPIXEL (sheet, column) + CELL_SPACING;
6568 if (sheet->row_titles_visible)
6569 allocation.x -= sheet->row_title_area.width;
6571 allocation.width = xxx_column_width (sheet, column);
6573 is_sensitive = xxx_column_is_sensitive (sheet, column);
6574 gtk_sheet_button_draw (sheet, window, button,
6575 is_sensitive, allocation);
6577 /* FIXME: Not freeing this button is correct (sort of),
6578 because in PSPP the model always returns a static copy */
6580 /* gtk_sheet_button_free (button); */
6586 gtk_sheet_row_title_button_draw (GtkSheet *sheet, gint row)
6588 GdkWindow *window = NULL;
6589 GdkRectangle allocation;
6590 GtkSheetButton *button = NULL;
6591 gboolean is_sensitive = FALSE;
6594 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
6596 if (row >= 0 && !yyy_row_is_visible (sheet, row)) return;
6597 if (row >= 0 && !sheet->row_titles_visible) return;
6598 if (row >= 0 && row < MIN_VISIBLE_ROW (sheet)) return;
6599 if (row >= 0 && row > MAX_VISIBLE_ROW (sheet)) return;
6602 window = sheet->row_title_window;
6603 button = yyy_row_button (sheet, row);
6605 allocation.y = ROW_TOP_YPIXEL (sheet, row) + CELL_SPACING;
6606 if (sheet->column_titles_visible)
6607 allocation.y -= sheet->column_title_area.height;
6608 allocation.width = sheet->row_title_area.width;
6609 allocation.height = yyy_row_height (sheet, row);
6610 is_sensitive = yyy_row_is_sensitive (sheet, row);
6612 gtk_sheet_button_draw (sheet, window, button, is_sensitive, allocation);
6619 * vadjustment_value_changed
6620 * hadjustment_value_changed */
6623 adjust_scrollbars (GtkSheet * sheet)
6625 if (sheet->vadjustment)
6627 sheet->vadjustment->page_size = sheet->sheet_window_height;
6628 sheet->vadjustment->page_increment = sheet->sheet_window_height / 2;
6629 sheet->vadjustment->step_increment = DEFAULT_ROW_HEIGHT (GTK_WIDGET (sheet));
6630 sheet->vadjustment->lower = 0;
6631 sheet->vadjustment->upper = SHEET_HEIGHT (sheet) + 80;
6632 g_signal_emit_by_name (sheet->vadjustment, "changed");
6636 if (sheet->hadjustment)
6638 sheet->hadjustment->page_size = sheet->sheet_window_width;
6639 sheet->hadjustment->page_increment = sheet->sheet_window_width / 2;
6640 sheet->hadjustment->step_increment = DEFAULT_COLUMN_WIDTH;
6641 sheet->hadjustment->lower = 0;
6642 sheet->hadjustment->upper = SHEET_WIDTH (sheet)+ 80;
6643 g_signal_emit_by_name (sheet->hadjustment, "changed");
6649 vadjustment_value_changed (GtkAdjustment * adjustment,
6653 gint diff, value, old_value;
6657 g_return_if_fail (adjustment != NULL);
6658 g_return_if_fail (data != NULL);
6659 g_return_if_fail (GTK_IS_SHEET (data));
6661 sheet = GTK_SHEET (data);
6663 if (GTK_SHEET_IS_FROZEN (sheet)) return;
6665 row = ROW_FROM_YPIXEL (sheet, sheet->column_title_area.height + CELL_SPACING);
6666 if (!sheet->column_titles_visible)
6667 row = ROW_FROM_YPIXEL (sheet, CELL_SPACING);
6669 old_value = - sheet->voffset;
6671 new_row = g_sheet_row_pixel_to_row (sheet->row_geometry,
6672 adjustment->value, sheet);
6674 y = g_sheet_row_start_pixel (sheet->row_geometry, new_row, sheet);
6676 if (adjustment->value > sheet->old_vadjustment && sheet->old_vadjustment > 0. &&
6677 yyy_row_height (sheet, row) > sheet->vadjustment->step_increment)
6679 /* This avoids embarrassing twitching */
6680 if (row == new_row && row != yyy_row_count (sheet) - 1 &&
6681 adjustment->value - sheet->old_vadjustment >=
6682 sheet->vadjustment->step_increment &&
6683 new_row + 1 != MIN_VISIBLE_ROW (sheet))
6686 y = y+yyy_row_height (sheet, row);
6690 /* Negative old_adjustment enforces the redraw, otherwise avoid
6692 if (sheet->old_vadjustment >= 0. && row == new_row)
6694 sheet->old_vadjustment = sheet->vadjustment->value;
6698 sheet->old_vadjustment = sheet->vadjustment->value;
6699 adjustment->value = y;
6704 sheet->vadjustment->step_increment = yyy_row_height (sheet, 0);
6708 sheet->vadjustment->step_increment =
6709 MIN (yyy_row_height (sheet, new_row), yyy_row_height (sheet, new_row - 1));
6712 sheet->vadjustment->value = adjustment->value;
6714 value = adjustment->value;
6716 if (value >= - sheet->voffset)
6719 diff = value + sheet->voffset;
6724 diff = - sheet->voffset - value;
6727 sheet->voffset = - value;
6729 if (GTK_WIDGET_REALIZED (sheet->entry_widget) &&
6730 sheet->state == GTK_SHEET_NORMAL &&
6731 sheet->active_cell.row >= 0 && sheet->active_cell.col >= 0 &&
6732 !gtk_sheet_cell_isvisible (sheet, sheet->active_cell.row,
6733 sheet->active_cell.col))
6737 text = gtk_entry_get_text (GTK_ENTRY (gtk_sheet_get_entry (sheet)));
6739 if (!text || strlen (text) == 0)
6740 gtk_sheet_cell_clear (sheet,
6741 sheet->active_cell.row,
6742 sheet->active_cell.col);
6743 gtk_widget_unmap (sheet->entry_widget);
6746 gtk_sheet_position_children (sheet);
6748 gtk_sheet_range_draw (sheet, NULL);
6749 size_allocate_row_title_buttons (sheet);
6750 size_allocate_global_button (sheet);
6754 hadjustment_value_changed (GtkAdjustment * adjustment,
6758 gint i, diff, value, old_value;
6759 gint column, new_column;
6762 g_return_if_fail (adjustment != NULL);
6763 g_return_if_fail (data != NULL);
6764 g_return_if_fail (GTK_IS_SHEET (data));
6766 sheet = GTK_SHEET (data);
6768 if (GTK_SHEET_IS_FROZEN (sheet)) return;
6770 column = COLUMN_FROM_XPIXEL (sheet, sheet->row_title_area.width + CELL_SPACING);
6771 if (!sheet->row_titles_visible)
6772 column = COLUMN_FROM_XPIXEL (sheet, CELL_SPACING);
6774 old_value = - sheet->hoffset;
6776 for (i = 0; i < xxx_column_count (sheet); i++)
6778 if (xxx_column_is_visible (sheet, i)) x += xxx_column_width (sheet, i);
6779 if (x > adjustment->value) break;
6781 x -= xxx_column_width (sheet, i);
6784 if (adjustment->value > sheet->old_hadjustment && sheet->old_hadjustment > 0 &&
6785 xxx_column_width (sheet, i) > sheet->hadjustment->step_increment)
6787 /* This avoids embarrassing twitching */
6788 if (column == new_column && column != xxx_column_count (sheet) - 1 &&
6789 adjustment->value - sheet->old_hadjustment >=
6790 sheet->hadjustment->step_increment &&
6791 new_column + 1 != MIN_VISIBLE_COLUMN (sheet))
6794 x += xxx_column_width (sheet, column);
6798 /* Negative old_adjustment enforces the redraw, otherwise avoid spureous redraw */
6799 if (sheet->old_hadjustment >= 0. && new_column == column)
6801 sheet->old_hadjustment = sheet->hadjustment->value;
6805 sheet->old_hadjustment = sheet->hadjustment->value;
6806 adjustment->value = x;
6808 if (new_column == 0)
6810 sheet->hadjustment->step_increment = xxx_column_width (sheet, 0);
6814 sheet->hadjustment->step_increment =
6815 MIN (xxx_column_width (sheet, new_column), xxx_column_width (sheet, new_column - 1));
6819 sheet->hadjustment->value = adjustment->value;
6821 value = adjustment->value;
6823 if (value >= - sheet->hoffset)
6826 diff = value + sheet->hoffset;
6831 diff = - sheet->hoffset - value;
6834 sheet->hoffset = - value;
6835 if (GTK_WIDGET_REALIZED (sheet->entry_widget) &&
6836 sheet->state == GTK_SHEET_NORMAL &&
6837 sheet->active_cell.row >= 0 && sheet->active_cell.col >= 0 &&
6838 !gtk_sheet_cell_isvisible (sheet, sheet->active_cell.row,
6839 sheet->active_cell.col))
6843 text = gtk_entry_get_text (GTK_ENTRY (gtk_sheet_get_entry (sheet)));
6844 if (!text || strlen (text) == 0)
6845 gtk_sheet_cell_clear (sheet,
6846 sheet->active_cell.row,
6847 sheet->active_cell.col);
6849 gtk_widget_unmap (sheet->entry_widget);
6852 gtk_sheet_position_children (sheet);
6854 gtk_sheet_range_draw (sheet, NULL);
6855 size_allocate_column_title_buttons (sheet);
6859 /* COLUMN RESIZING */
6861 draw_xor_vline (GtkSheet * sheet)
6865 g_return_if_fail (sheet != NULL);
6867 widget = GTK_WIDGET (sheet);
6869 gdk_draw_line (widget->window, sheet->xor_gc,
6871 sheet->column_title_area.height,
6873 sheet->sheet_window_height + 1);
6878 draw_xor_hline (GtkSheet * sheet)
6882 g_return_if_fail (sheet != NULL);
6884 widget = GTK_WIDGET (sheet);
6886 gdk_draw_line (widget->window, sheet->xor_gc,
6887 sheet->row_title_area.width,
6890 sheet->sheet_window_width + 1,
6894 /* SELECTED RANGE */
6896 draw_xor_rectangle (GtkSheet *sheet, GtkSheetRange range)
6899 GdkRectangle clip_area, area;
6902 area.x = COLUMN_LEFT_XPIXEL (sheet, range.col0);
6903 area.y = ROW_TOP_YPIXEL (sheet, range.row0);
6904 area.width = COLUMN_LEFT_XPIXEL (sheet, range.coli)- area.x+
6905 xxx_column_width (sheet, range.coli);
6906 area.height = ROW_TOP_YPIXEL (sheet, range.rowi)- area.y+
6907 yyy_row_height (sheet, range.rowi);
6909 clip_area.x = sheet->row_title_area.width;
6910 clip_area.y = sheet->column_title_area.height;
6911 clip_area.width = sheet->sheet_window_width;
6912 clip_area.height = sheet->sheet_window_height;
6914 if (!sheet->row_titles_visible) clip_area.x = 0;
6915 if (!sheet->column_titles_visible) clip_area.y = 0;
6919 area.width = area.width + area.x;
6922 if (area.width > clip_area.width) area.width = clip_area.width + 10;
6925 area.height = area.height + area.y;
6928 if (area.height > clip_area.height) area.height = clip_area.height + 10;
6932 clip_area.width += 3;
6933 clip_area.height += 3;
6935 gdk_gc_get_values (sheet->xor_gc, &values);
6937 gdk_gc_set_clip_rectangle (sheet->xor_gc, &clip_area);
6939 for (i = -1; i <= 1; ++i)
6940 gdk_draw_rectangle (sheet->sheet_window,
6943 area.x + i, area.y + i,
6944 area.width - 2 * i, area.height - 2 * i);
6947 gdk_gc_set_clip_rectangle (sheet->xor_gc, NULL);
6949 gdk_gc_set_foreground (sheet->xor_gc, &values.foreground);
6954 /* this function returns the new width of the column being resized given
6955 * the column and x position of the cursor; the x cursor position is passed
6956 * in as a pointer and automaticaly corrected if it's beyond min / max limits */
6958 new_column_width (GtkSheet * sheet,
6967 min_width = sheet->column_requisition;
6969 /* you can't shrink a column to less than its minimum width */
6970 if (cx < COLUMN_LEFT_XPIXEL (sheet, column) + min_width)
6972 *x = cx = COLUMN_LEFT_XPIXEL (sheet, column) + min_width;
6975 /* calculate new column width making sure it doesn't end up
6976 * less than the minimum width */
6977 width = cx - COLUMN_LEFT_XPIXEL (sheet, column);
6978 if (width < min_width)
6981 xxx_set_column_width (sheet, column, width);
6982 size_allocate_column_title_buttons (sheet);
6987 /* this function returns the new height of the row being resized given
6988 * the row and y position of the cursor; the y cursor position is passed
6989 * in as a pointer and automaticaly corrected if it's beyond min / max limits */
6991 new_row_height (GtkSheet * sheet,
6999 min_height = sheet->row_requisition;
7001 /* you can't shrink a row to less than its minimum height */
7002 if (cy < ROW_TOP_YPIXEL (sheet, row) + min_height)
7005 *y = cy = ROW_TOP_YPIXEL (sheet, row) + min_height;
7008 /* calculate new row height making sure it doesn't end up
7009 * less than the minimum height */
7010 height = (cy - ROW_TOP_YPIXEL (sheet, row));
7011 if (height < min_height)
7012 height = min_height;
7014 yyy_set_row_height (sheet, row, height);
7015 size_allocate_row_title_buttons (sheet);
7021 gtk_sheet_set_column_width (GtkSheet * sheet,
7027 g_return_if_fail (sheet != NULL);
7028 g_return_if_fail (GTK_IS_SHEET (sheet));
7030 if (column < 0 || column >= xxx_column_count (sheet))
7033 gtk_sheet_column_size_request (sheet, column, &min_width);
7034 if (width < min_width) return;
7036 xxx_set_column_width (sheet, column, width);
7038 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)) && !GTK_SHEET_IS_FROZEN (sheet))
7040 size_allocate_column_title_buttons (sheet);
7041 adjust_scrollbars (sheet);
7042 gtk_sheet_size_allocate_entry (sheet);
7043 gtk_sheet_range_draw (sheet, NULL);
7046 g_signal_emit (sheet, sheet_signals[CHANGED], 0, -1, column);
7052 gtk_sheet_set_row_height (GtkSheet * sheet,
7058 g_return_if_fail (sheet != NULL);
7059 g_return_if_fail (GTK_IS_SHEET (sheet));
7061 if (row < 0 || row >= yyy_row_count (sheet))
7064 gtk_sheet_row_size_request (sheet, row, &min_height);
7065 if (height < min_height) return;
7067 yyy_set_row_height (sheet, row, height);
7069 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)) && !GTK_SHEET_IS_FROZEN (sheet))
7071 size_allocate_row_title_buttons (sheet);
7072 adjust_scrollbars (sheet);
7073 gtk_sheet_size_allocate_entry (sheet);
7074 gtk_sheet_range_draw (sheet, NULL);
7077 g_signal_emit (sheet, sheet_signals[CHANGED], 0, row, - 1);
7080 gtk_sheet_get_attributes (const GtkSheet *sheet, gint row, gint col,
7081 GtkSheetCellAttr *attributes)
7083 const GdkColor *fg, *bg;
7084 const GtkJustification *j ;
7085 const PangoFontDescription *font_desc ;
7086 const GtkSheetCellBorder *border ;
7088 g_return_val_if_fail (sheet != NULL, FALSE);
7089 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
7091 if (row < 0 || col < 0) return FALSE;
7093 init_attributes (sheet, col, attributes);
7098 attributes->is_editable = g_sheet_model_is_editable (sheet->model, row, col);
7099 attributes->is_visible = g_sheet_model_is_visible (sheet->model, row, col);
7101 fg = g_sheet_model_get_foreground (sheet->model, row, col);
7103 attributes->foreground = *fg;
7105 bg = g_sheet_model_get_background (sheet->model, row, col);
7107 attributes->background = *bg;
7109 j = g_sheet_model_get_justification (sheet->model, row, col);
7110 if (j) attributes->justification = *j;
7112 font_desc = g_sheet_model_get_font_desc (sheet->model, row, col);
7113 if ( font_desc ) attributes->font_desc = font_desc;
7115 border = g_sheet_model_get_cell_border (sheet->model, row, col);
7117 if ( border ) attributes->border = *border;
7123 init_attributes (const GtkSheet *sheet, gint col, GtkSheetCellAttr *attributes)
7125 /* DEFAULT VALUES */
7126 attributes->foreground = GTK_WIDGET (sheet)->style->black;
7127 attributes->background = sheet->bg_color;
7128 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
7130 GdkColormap *colormap;
7131 colormap = gdk_colormap_get_system ();
7132 gdk_color_black (colormap, &attributes->foreground);
7133 attributes->background = sheet->bg_color;
7135 attributes->justification = xxx_column_justification (sheet, col);
7136 attributes->border.width = 0;
7137 attributes->border.line_style = GDK_LINE_SOLID;
7138 attributes->border.cap_style = GDK_CAP_NOT_LAST;
7139 attributes->border.join_style = GDK_JOIN_MITER;
7140 attributes->border.mask = 0;
7141 attributes->border.color = GTK_WIDGET (sheet)->style->black;
7142 attributes->is_editable = TRUE;
7143 attributes->is_visible = TRUE;
7144 attributes->font_desc = GTK_WIDGET (sheet)->style->font_desc;
7148 /********************************************************************
7149 * Container Functions:
7154 * gtk_sheet_move_child
7155 * gtk_sheet_position_child
7156 * gtk_sheet_position_children
7157 * gtk_sheet_realize_child
7158 * gtk_sheet_get_child_at
7159 ********************************************************************/
7162 gtk_sheet_put (GtkSheet *sheet, GtkWidget *child, gint x, gint y)
7164 GtkRequisition child_requisition;
7165 GtkSheetChild *child_info;
7167 g_return_val_if_fail (sheet != NULL, NULL);
7168 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
7169 g_return_val_if_fail (child != NULL, NULL);
7170 g_return_val_if_fail (child->parent == NULL, NULL);
7172 child_info = g_new (GtkSheetChild, 1);
7173 child_info->widget = child;
7176 child_info->attached_to_cell = FALSE;
7177 child_info->floating = TRUE;
7178 child_info->xpadding = child_info->ypadding = 0;
7179 child_info->xexpand = child_info->yexpand = FALSE;
7180 child_info->xshrink = child_info->yshrink = FALSE;
7181 child_info->xfill = child_info->yfill = FALSE;
7183 sheet->children = g_list_append (sheet->children, child_info);
7185 gtk_widget_set_parent (child, GTK_WIDGET (sheet));
7187 gtk_widget_size_request (child, &child_requisition);
7189 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (sheet)))
7191 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)) &&
7192 (!GTK_WIDGET_REALIZED (child) || GTK_WIDGET_NO_WINDOW (child)))
7193 gtk_sheet_realize_child (sheet, child_info);
7195 if (GTK_WIDGET_MAPPED (GTK_WIDGET (sheet)) &&
7196 !GTK_WIDGET_MAPPED (child))
7197 gtk_widget_map (child);
7200 gtk_sheet_position_child (sheet, child_info);
7202 /* This will avoid drawing on the titles */
7204 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
7206 if (sheet->row_titles_visible)
7207 gdk_window_show (sheet->row_title_window);
7208 if (sheet->column_titles_visible)
7209 gdk_window_show (sheet->column_title_window);
7212 return (child_info);
7216 gtk_sheet_attach_floating (GtkSheet *sheet,
7221 GtkSheetChild *child;
7223 if (row < 0 || col < 0)
7225 gtk_sheet_button_attach (sheet, widget, row, col);
7229 gtk_sheet_get_cell_area (sheet, row, col, &area);
7230 child = gtk_sheet_put (sheet, widget, area.x, area.y);
7231 child->attached_to_cell = TRUE;
7237 gtk_sheet_attach_default (GtkSheet *sheet,
7241 if (row < 0 || col < 0)
7243 gtk_sheet_button_attach (sheet, widget, row, col);
7247 gtk_sheet_attach (sheet, widget, row, col,
7248 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
7252 gtk_sheet_attach (GtkSheet *sheet,
7261 GtkSheetChild *child = NULL;
7263 if (row < 0 || col < 0)
7265 gtk_sheet_button_attach (sheet, widget, row, col);
7269 child = g_new0 (GtkSheetChild, 1);
7270 child->attached_to_cell = TRUE;
7271 child->floating = FALSE;
7272 child->widget = widget;
7275 child->xpadding = xpadding;
7276 child->ypadding = ypadding;
7277 child->xexpand = (xoptions & GTK_EXPAND) != 0;
7278 child->yexpand = (yoptions & GTK_EXPAND) != 0;
7279 child->xshrink = (xoptions & GTK_SHRINK) != 0;
7280 child->yshrink = (yoptions & GTK_SHRINK) != 0;
7281 child->xfill = (xoptions & GTK_FILL) != 0;
7282 child->yfill = (yoptions & GTK_FILL) != 0;
7284 sheet->children = g_list_append (sheet->children, child);
7286 gtk_sheet_get_cell_area (sheet, row, col, &area);
7288 child->x = area.x + child->xpadding;
7289 child->y = area.y + child->ypadding;
7291 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (sheet)))
7293 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)) &&
7294 (!GTK_WIDGET_REALIZED (widget) || GTK_WIDGET_NO_WINDOW (widget)))
7295 gtk_sheet_realize_child (sheet, child);
7297 if (GTK_WIDGET_MAPPED (GTK_WIDGET (sheet)) &&
7298 !GTK_WIDGET_MAPPED (widget))
7299 gtk_widget_map (widget);
7302 gtk_sheet_position_child (sheet, child);
7304 /* This will avoid drawing on the titles */
7306 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
7308 if (GTK_SHEET_ROW_TITLES_VISIBLE (sheet))
7309 gdk_window_show (sheet->row_title_window);
7310 if (GTK_SHEET_COL_TITLES_VISIBLE (sheet))
7311 gdk_window_show (sheet->column_title_window);
7317 gtk_sheet_button_attach (GtkSheet *sheet,
7321 GtkSheetButton *button = 0;
7322 GtkSheetChild *child;
7323 GtkRequisition button_requisition;
7325 if (row >= 0 && col >= 0) return;
7326 if (row < 0 && col < 0) return;
7328 child = g_new (GtkSheetChild, 1);
7329 child->widget = widget;
7332 child->attached_to_cell = TRUE;
7333 child->floating = FALSE;
7336 child->xpadding = child->ypadding = 0;
7337 child->xshrink = child->yshrink = FALSE;
7338 child->xfill = child->yfill = FALSE;
7341 sheet->children = g_list_append (sheet->children, child);
7343 gtk_sheet_button_size_request (sheet, button, &button_requisition);
7346 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (sheet)))
7348 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)) &&
7349 (!GTK_WIDGET_REALIZED (widget) || GTK_WIDGET_NO_WINDOW (widget)))
7350 gtk_sheet_realize_child (sheet, child);
7352 if (GTK_WIDGET_MAPPED (GTK_WIDGET (sheet)) &&
7353 !GTK_WIDGET_MAPPED (widget))
7354 gtk_widget_map (widget);
7357 if (row == -1) size_allocate_column_title_buttons (sheet);
7358 if (col == -1) size_allocate_row_title_buttons (sheet);
7363 label_size_request (GtkSheet *sheet, gchar *label, GtkRequisition *req)
7368 gint row_height = DEFAULT_ROW_HEIGHT (GTK_WIDGET (sheet)) - 2 * CELLOFFSET + 2;
7374 while (words && *words != '\0')
7376 if (*words == '\n' || * (words + 1) == '\0')
7378 req->height += row_height;
7381 req->width = MAX (req->width, STRING_WIDTH (GTK_WIDGET (sheet), GTK_WIDGET (sheet)->style->font_desc, word));
7391 if (n > 0) req->height -= 2;
7395 gtk_sheet_button_size_request (GtkSheet *sheet,
7396 const GtkSheetButton *button,
7397 GtkRequisition *button_requisition)
7399 GtkRequisition requisition;
7400 GtkRequisition label_requisition;
7402 if (gtk_sheet_autoresize (sheet) && button->label && strlen (button->label) > 0)
7404 label_size_request (sheet, button->label, &label_requisition);
7405 label_requisition.width += 2 * CELLOFFSET;
7406 label_requisition.height += 2 * CELLOFFSET;
7410 label_requisition.height = DEFAULT_ROW_HEIGHT (GTK_WIDGET (sheet));
7411 label_requisition.width = COLUMN_MIN_WIDTH;
7416 gtk_widget_size_request (button->child->widget, &requisition);
7417 requisition.width += 2 * button->child->xpadding;
7418 requisition.height += 2 * button->child->ypadding;
7419 requisition.width += 2 * sheet->button->style->xthickness;
7420 requisition.height += 2 * sheet->button->style->ythickness;
7424 requisition.height = DEFAULT_ROW_HEIGHT (GTK_WIDGET (sheet));
7425 requisition.width = COLUMN_MIN_WIDTH;
7428 *button_requisition = requisition;
7429 button_requisition->width = MAX (requisition.width, label_requisition.width);
7430 button_requisition->height = MAX (requisition.height, label_requisition.height);
7435 gtk_sheet_row_size_request (GtkSheet *sheet,
7439 GtkRequisition button_requisition;
7442 gtk_sheet_button_size_request (sheet,
7443 yyy_row_button (sheet, row),
7444 &button_requisition);
7446 *requisition = button_requisition.height;
7448 children = sheet->children;
7451 GtkSheetChild *child = (GtkSheetChild *)children->data;
7452 GtkRequisition child_requisition;
7454 if (child->attached_to_cell && child->row == row && child->col != -1 && !child->floating && !child->yshrink)
7456 gtk_widget_get_child_requisition (child->widget, &child_requisition);
7458 if (child_requisition.height + 2 * child->ypadding > *requisition)
7459 *requisition = child_requisition.height + 2 * child->ypadding;
7461 children = children->next;
7464 sheet->row_requisition = * requisition;
7468 gtk_sheet_column_size_request (GtkSheet *sheet,
7472 GtkRequisition button_requisition;
7474 GtkSheetButton *button = xxx_column_button (sheet, col);
7476 gtk_sheet_button_size_request (sheet,
7478 &button_requisition);
7480 gtk_sheet_button_free (button);
7482 *requisition = button_requisition.width;
7484 children = sheet->children;
7487 GtkSheetChild *child = (GtkSheetChild *)children->data;
7488 GtkRequisition child_requisition;
7490 if (child->attached_to_cell && child->col == col && child->row != -1 && !child->floating && !child->xshrink)
7492 gtk_widget_get_child_requisition (child->widget, &child_requisition);
7494 if (child_requisition.width + 2 * child->xpadding > *requisition)
7495 *requisition = child_requisition.width + 2 * child->xpadding;
7497 children = children->next;
7500 sheet->column_requisition = *requisition;
7504 gtk_sheet_move_child (GtkSheet *sheet, GtkWidget *widget, gint x, gint y)
7506 GtkSheetChild *child;
7509 g_return_if_fail (sheet != NULL);
7510 g_return_if_fail (GTK_IS_SHEET (sheet));
7512 children = sheet->children;
7515 child = children->data;
7517 if (child->widget == widget)
7521 child->row = ROW_FROM_YPIXEL (sheet, y);
7522 child->col = COLUMN_FROM_XPIXEL (sheet, x);
7523 gtk_sheet_position_child (sheet, child);
7527 children = children->next;
7530 g_warning ("Widget must be a GtkSheet child");
7535 gtk_sheet_position_child (GtkSheet *sheet, GtkSheetChild *child)
7537 GtkRequisition child_requisition;
7538 GtkAllocation child_allocation;
7544 gtk_widget_get_child_requisition (child->widget, &child_requisition);
7546 if (sheet->column_titles_visible)
7547 yoffset = sheet->column_title_area.height;
7549 if (sheet->row_titles_visible)
7550 xoffset = sheet->row_title_area.width;
7552 if (child->attached_to_cell)
7554 gtk_sheet_get_cell_area (sheet, child->row, child->col, &area);
7555 child->x = area.x + child->xpadding;
7556 child->y = area.y + child->ypadding;
7558 if (!child->floating)
7560 if (child_requisition.width + 2 * child->xpadding <= xxx_column_width (sheet, child->col))
7564 child_requisition.width = child_allocation.width = xxx_column_width (sheet, child->col) - 2 * child->xpadding;
7570 child->x = area.x + xxx_column_width (sheet, child->col) / 2 -
7571 child_requisition.width / 2;
7573 child_allocation.width = child_requisition.width;
7578 if (!child->xshrink)
7580 gtk_sheet_set_column_width (sheet, child->col, child_requisition.width + 2 * child->xpadding);
7582 child_allocation.width = xxx_column_width (sheet, child->col) - 2 * child->xpadding;
7585 if (child_requisition.height +
7586 2 * child->ypadding <= yyy_row_height (sheet, child->row))
7590 child_requisition.height = child_allocation.height =
7591 yyy_row_height (sheet, child->row) - 2 * child->ypadding;
7597 child->y = area.y + yyy_row_height (sheet, child->row) / 2
7598 - child_requisition.height / 2;
7600 child_allocation.height = child_requisition.height;
7605 if (!child->yshrink)
7607 gtk_sheet_set_row_height (sheet, child->row, child_requisition.height + 2 * child->ypadding);
7609 child_allocation.height = yyy_row_height (sheet, child->row) -
7610 2 * child->ypadding;
7615 child_allocation.width = child_requisition.width;
7616 child_allocation.height = child_requisition.height;
7619 x = child_allocation.x = child->x + xoffset;
7620 y = child_allocation.y = child->y + yoffset;
7624 x = child_allocation.x = child->x + sheet->hoffset + xoffset;
7625 x = child_allocation.x = child->x + xoffset;
7626 y = child_allocation.y = child->y + sheet->voffset + yoffset;
7627 y = child_allocation.y = child->y + yoffset;
7628 child_allocation.width = child_requisition.width;
7629 child_allocation.height = child_requisition.height;
7632 gtk_widget_size_allocate (child->widget, &child_allocation);
7633 gtk_widget_queue_draw (child->widget);
7637 gtk_sheet_forall (GtkContainer *container,
7638 gboolean include_internals,
7639 GtkCallback callback,
7640 gpointer callback_data)
7643 GtkSheetChild *child;
7646 g_return_if_fail (GTK_IS_SHEET (container));
7647 g_return_if_fail (callback != NULL);
7649 sheet = GTK_SHEET (container);
7650 children = sheet->children;
7653 child = children->data;
7654 children = children->next;
7656 (* callback) (child->widget, callback_data);
7659 if (sheet->button && sheet->button->parent)
7660 (* callback) (sheet->button, callback_data);
7662 if (sheet->entry_container && GTK_IS_CONTAINER (sheet->entry_container))
7663 (* callback) (sheet->entry_container, callback_data);
7668 gtk_sheet_position_children (GtkSheet *sheet)
7671 GtkSheetChild *child;
7673 children = sheet->children;
7677 child = (GtkSheetChild *)children->data;
7679 if (child->col != -1 && child->row != -1)
7680 gtk_sheet_position_child (sheet, child);
7682 if (child->row == -1)
7684 if (child->col < MIN_VISIBLE_COLUMN (sheet) ||
7685 child->col > MAX_VISIBLE_COLUMN (sheet))
7686 gtk_sheet_child_hide (child);
7688 gtk_sheet_child_show (child);
7690 if (child->col == -1)
7692 if (child->row < MIN_VISIBLE_ROW (sheet) ||
7693 child->row > MAX_VISIBLE_ROW (sheet))
7694 gtk_sheet_child_hide (child);
7696 gtk_sheet_child_show (child);
7699 children = children->next;
7704 gtk_sheet_remove (GtkContainer *container, GtkWidget *widget)
7708 GtkSheetChild *child = 0;
7710 g_return_if_fail (container != NULL);
7711 g_return_if_fail (GTK_IS_SHEET (container));
7713 sheet = GTK_SHEET (container);
7715 children = sheet->children;
7719 child = (GtkSheetChild *)children->data;
7721 if (child->widget == widget) break;
7723 children = children->next;
7728 gtk_widget_unparent (widget);
7729 child->widget = NULL;
7731 sheet->children = g_list_remove_link (sheet->children, children);
7732 g_list_free_1 (children);
7736 gtk_widget_unparent (sheet->button);
7740 gtk_sheet_realize_child (GtkSheet *sheet, GtkSheetChild *child)
7744 widget = GTK_WIDGET (sheet);
7746 if (GTK_WIDGET_REALIZED (widget))
7748 if (child->row == -1)
7749 gtk_widget_set_parent_window (child->widget, sheet->column_title_window);
7750 else if (child->col == -1)
7751 gtk_widget_set_parent_window (child->widget, sheet->row_title_window);
7753 gtk_widget_set_parent_window (child->widget, sheet->sheet_window);
7756 gtk_widget_set_parent (child->widget, widget);
7762 gtk_sheet_get_child_at (GtkSheet *sheet, gint row, gint col)
7765 GtkSheetChild *child = 0;
7767 g_return_val_if_fail (sheet != NULL, NULL);
7768 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
7770 children = sheet->children;
7774 child = (GtkSheetChild *)children->data;
7776 if (child->attached_to_cell)
7777 if (child->row == row && child->col == col) break;
7779 children = children->next;
7782 if (children) return child;
7788 gtk_sheet_child_hide (GtkSheetChild *child)
7790 g_return_if_fail (child != NULL);
7791 gtk_widget_hide (child->widget);
7795 gtk_sheet_child_show (GtkSheetChild *child)
7797 g_return_if_fail (child != NULL);
7799 gtk_widget_show (child->widget);
7803 gtk_sheet_get_model (const GtkSheet *sheet)
7805 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
7807 return sheet->model;
7812 gtk_sheet_button_new (void)
7814 GtkSheetButton *button = g_malloc (sizeof (GtkSheetButton));
7816 button->state = GTK_STATE_NORMAL;
7817 button->label = NULL;
7818 button->label_visible = TRUE;
7819 button->child = NULL;
7820 button->justification = GTK_JUSTIFY_FILL;
7827 gtk_sheet_button_free (GtkSheetButton *button)
7829 if (!button) return ;
7831 g_free (button->label);
7837 range_to_text (const GtkSheet *sheet)
7839 gchar *celltext = NULL;
7843 if ( !gtk_sheet_range_isvisible (sheet, sheet->range))
7846 string = g_string_sized_new (80);
7848 for (r = sheet->range.row0; r <= sheet->range.rowi; ++r)
7850 for (c = sheet->range.col0; c < sheet->range.coli; ++c)
7852 celltext = gtk_sheet_cell_get_text (sheet, r, c);
7853 g_string_append (string, celltext);
7854 g_string_append (string, "\t");
7857 celltext = gtk_sheet_cell_get_text (sheet, r, c);
7858 g_string_append (string, celltext);
7859 if ( r < sheet->range.rowi)
7860 g_string_append (string, "\n");
7868 range_to_html (const GtkSheet *sheet)
7870 gchar *celltext = NULL;
7874 if ( !gtk_sheet_range_isvisible (sheet, sheet->range))
7877 string = g_string_sized_new (480);
7879 g_string_append (string, "<html>\n");
7880 g_string_append (string, "<body>\n");
7881 g_string_append (string, "<table>\n");
7882 for (r = sheet->range.row0; r <= sheet->range.rowi; ++r)
7884 g_string_append (string, "<tr>\n");
7885 for (c = sheet->range.col0; c <= sheet->range.coli; ++c)
7887 g_string_append (string, "<td>");
7888 celltext = gtk_sheet_cell_get_text (sheet, r, c);
7889 g_string_append (string, celltext);
7890 g_string_append (string, "</td>\n");
7893 g_string_append (string, "</tr>\n");
7895 g_string_append (string, "</table>\n");
7896 g_string_append (string, "</body>\n");
7897 g_string_append (string, "</html>\n");
7909 primary_get_cb (GtkClipboard *clipboard,
7910 GtkSelectionData *selection_data,
7914 GtkSheet *sheet = GTK_SHEET (data);
7915 GString *string = NULL;
7919 case SELECT_FMT_TEXT:
7920 string = range_to_text (sheet);
7922 case SELECT_FMT_HTML:
7923 string = range_to_html (sheet);
7926 g_assert_not_reached ();
7929 gtk_selection_data_set (selection_data, selection_data->target,
7931 (const guchar *) string->str, string->len);
7932 g_string_free (string, TRUE);
7936 primary_clear_cb (GtkClipboard *clipboard,
7939 GtkSheet *sheet = GTK_SHEET (data);
7940 gtk_sheet_real_unselect_range (sheet, NULL);
7944 gtk_sheet_update_primary_selection (GtkSheet *sheet)
7946 static const GtkTargetEntry targets[] = {
7947 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
7948 { "STRING", 0, SELECT_FMT_TEXT },
7949 { "TEXT", 0, SELECT_FMT_TEXT },
7950 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
7951 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
7952 { "text/plain", 0, SELECT_FMT_TEXT },
7953 { "text/html", 0, SELECT_FMT_HTML }
7956 GtkClipboard *clipboard;
7958 if (!GTK_WIDGET_REALIZED (sheet))
7961 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (sheet),
7962 GDK_SELECTION_PRIMARY);
7964 if (gtk_sheet_range_isvisible (sheet, sheet->range))
7966 if (!gtk_clipboard_set_with_owner (clipboard, targets,
7967 G_N_ELEMENTS (targets),
7968 primary_get_cb, primary_clear_cb,
7970 primary_clear_cb (clipboard, sheet);
7974 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (sheet))
7975 gtk_clipboard_clear (clipboard);