From 5ba51e992d6f702cccdcc42402bc3086776d00d1 Mon Sep 17 00:00:00 2001 From: Miguel de Icaza Date: Mon, 15 Nov 1999 20:59:29 +0000 Subject: Add resizing capabilities. 1999-11-14 Miguel de Icaza * e-table-header-item.c (is_pointer_on_division): Add resizing capabilities. * e-table-sorted.c: Finish implementation. 1999-11-13 Miguel de Icaza * e-table-sorted.c: Implement e-table-sorted object. 1999-11-12 Miguel de Icaza * e-table-header-item.c: Make the thing configurable. * e-table-header-item.h: Add font field, location, height. svn path=/trunk/; revision=1388 --- widgets/table/e-table-header-item.c | 303 +++++++++++++++++++++++++++++++----- widgets/table/e-table-header-item.h | 11 ++ widgets/table/e-table-header.c | 1 - widgets/table/e-table-header.h | 1 + widgets/table/e-table-model.c | 20 +++ widgets/table/e-table-model.h | 1 + widgets/table/e-table-sorted.c | 90 +++++++++++ widgets/table/e-table-sorted.h | 34 ++++ widgets/table/table-test.c | 24 ++- 9 files changed, 447 insertions(+), 38 deletions(-) create mode 100644 widgets/table/e-table-sorted.c create mode 100644 widgets/table/e-table-sorted.h (limited to 'widgets/table') diff --git a/widgets/table/e-table-header-item.c b/widgets/table/e-table-header-item.c index effdbb3f0b..50b0b6a677 100644 --- a/widgets/table/e-table-header-item.c +++ b/widgets/table/e-table-header-item.c @@ -1,16 +1,23 @@ /* - * E-table-column-view.c: A canvas view of the TableColumn. + * E-table-column-view.c: A canvas item based view of the ETableColumn. * * Author: * Miguel de Icaza (miguel@gnu.org) * - * Copyright 1999, International GNOME Support. + * Copyright 1999, Helix Code, Inc. */ #include #include "e-table-header.h" #include "e-table-header-item.h" +#include "e-cursors.h" -#define ETHI_HEIGHT 14 +/* Padding above and below of the string in the header display */ +#define PADDING 4 + +/* Defines the tolerance for proximity of the column division to the cursor position */ +#define TOLERANCE 2 + +#define ETHI_RESIZING(x) ((x)->resize_col != -1) #define PARENT_OBJECT_TYPE gnome_canvas_item_get_type () @@ -18,7 +25,10 @@ static GnomeCanvasItemClass *ethi_parent_class; enum { ARG_0, - ARG_TABLE_HEADER + ARG_TABLE_HEADER, + ARG_TABLE_X, + ARG_TABLE_Y, + ARG_TABLE_FONTSET }; static void @@ -35,16 +45,32 @@ ethi_destroy (GtkObject *object) static void ethi_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags) { + ETableHeaderItem *ethi = E_TABLE_HEADER_ITEM (item); + if (GNOME_CANVAS_ITEM_CLASS (ethi_parent_class)->update) (*GNOME_CANVAS_ITEM_CLASS (ethi_parent_class)->update)(item, affine, clip_path, flags); - item->x1 = 0; - item->y1 = 0; + item->x1 = ethi->x1; + item->y1 = ethi->y1; item->x2 = INT_MAX; - item->y2 = INT_MAX; + item->y2 = ethi->x1 + ethi->height; + gnome_canvas_group_child_bounds (GNOME_CANVAS_GROUP (item->parent), item); } +static void +ethi_font_load (ETableHeaderItem *ethi, char *font) +{ + if (ethi->font) + gdk_font_unref (ethi->font); + + ethi->font = gdk_fontset_load (font); + if (ethi->font == NULL) + ethi->font = gdk_font_load ("fixed"); + + ethi->height = ethi->font->ascent + ethi->font->descent + PADDING; +} + static void ethi_set_arg (GtkObject *o, GtkArg *arg, guint arg_id) { @@ -59,6 +85,19 @@ ethi_set_arg (GtkObject *o, GtkArg *arg, guint arg_id) case ARG_TABLE_HEADER: ethi->eth = GTK_VALUE_POINTER (*arg); break; + + case ARG_TABLE_X: + ethi->x1 = GTK_VALUE_INT (*arg); + break; + + case ARG_TABLE_Y: + ethi->y1 = GTK_VALUE_INT (*arg); + break; + + case ARG_TABLE_FONTSET: + ethi_font_load (ethi, GTK_VALUE_STRING (*arg)); + break; + } ethi_update (item, NULL, NULL, 0); } @@ -80,6 +119,9 @@ ethi_realize (GnomeCanvasItem *item) gdk_gc_set_foreground (ethi->gc, &c); ethi->normal_cursor = gdk_cursor_new (GDK_ARROW); + + if (!ethi->font) + ethi_font_load (ethi, "fixed"); } static void @@ -100,6 +142,45 @@ ethi_unrealize (GnomeCanvasItem *item) (*GNOME_CANVAS_ITEM_CLASS (ethi_parent_class)->unrealize)(item); } +static void +draw_button (ETableHeaderItem *ethi, ETableCol *col, + GdkDrawable *drawable, GdkGC *gc, GtkStyle *style, + int x, int y, int width, int height) +{ + GdkRectangle clip; + int xtra; + + gdk_draw_rectangle ( + drawable, gc, TRUE, + x + 1, y + 1, width - 2, height -2); + + gtk_draw_shadow ( + style, drawable, + GTK_STATE_NORMAL, GTK_SHADOW_OUT, + x , y, width, height); + + clip.x = x + 2; + clip.y = y + 2; + clip.width = width - 4; + clip.height = ethi->height; + + gdk_gc_set_clip_rectangle (ethi->gc, &clip); + + /* Center the thing */ + xtra = (clip.width - gdk_string_measure (ethi->font, col->id))/2; + + if (xtra < 0) + xtra = 0; + + /* Skip over border */ + x += xtra + 2; + + gdk_draw_text ( + drawable, ethi->font, + ethi->gc, x, y + ethi->height - PADDING, + col->id, strlen (col->id)); +} + static void ethi_draw (GnomeCanvasItem *item, GdkDrawable *drawable, int x1, int y1, int width, int height) { @@ -112,11 +193,17 @@ ethi_draw (GnomeCanvasItem *item, GdkDrawable *drawable, int x1, int y1, int wid int x; total = 0; - x = 0; + x = -x1; + for (col = 0; col < cols; col++){ ETableCol *ecol = e_table_header_get_column (ethi->eth, col); - const int col_width = ecol->width; + int col_width; + if (col == ethi->resize_col) + col_width = ethi->resize_width; + else + col_width = ecol->width; + if (x1 > total + col_width){ x += col_width; continue; @@ -127,32 +214,11 @@ ethi_draw (GnomeCanvasItem *item, GdkDrawable *drawable, int x1, int y1, int wid gc = GTK_WIDGET (canvas)->style->bg_gc [GTK_STATE_ACTIVE]; - gdk_draw_rectangle ( - drawable, gc, TRUE, - x + 1, -y1 + 1, col_width - 2, ETHI_HEIGHT - 2); - - gtk_draw_shadow ( - GTK_WIDGET (canvas)->style, drawable, - GTK_STATE_NORMAL, GTK_SHADOW_OUT, - x , -y1, col_width, ETHI_HEIGHT); - - { - GdkRectangle clip; - GdkFont *font = GTK_WIDGET (canvas)->style->font; - - clip.x = x + 2; - clip.y = -y1 + 2; - clip.width = col_width - 4; - clip.height = ETHI_HEIGHT - 4; - gdk_gc_set_clip_rectangle (gc, &clip); + draw_button (ethi, ecol, drawable, gc, + GTK_WIDGET (canvas)->style, + x, -y1, col_width, ethi->height); - /* - * FIXME: should be obvious - */ - gdk_draw_text (drawable, font, gc, x, -y1 + 10, "TEST", 4); - - gdk_gc_set_clip_rectangle (gc, NULL); - } + x += col_width; } } @@ -164,10 +230,167 @@ ethi_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, return 0.0; } +/* + * is_pointer_on_division: + * + * Returns whether @pos is a column header division; If @the_total is not NULL, + * then the actual position is returned here. If @return_ecol is not NULL, + * then the ETableCol that actually contains this point is returned here + */ +static gboolean +is_pointer_on_division (ETableHeaderItem *ethi, int pos, int *the_total, int *return_col) +{ + const int cols = e_table_header_count (ethi->eth); + int col, total; + + total = 0; + for (col = 0; col < cols; col++){ + ETableCol *ecol = e_table_header_get_column (ethi->eth, col); + + total += ecol->width; + + if ((total - TOLERANCE < pos ) && (pos < total + TOLERANCE)){ + if (return_col) + *return_col = col; + if (the_total) + *the_total = total; + + return TRUE; + } + + if (total > pos + TOLERANCE) + return FALSE; + } + + return FALSE; +} + +#define convert(c,sx,sy,x,y) gnome_canvas_w2c (c,sx,sy,x,y) + +static void +set_cursor (ETableHeaderItem *ethi, int pos) +{ + GtkWidget *canvas = GTK_WIDGET (GNOME_CANVAS_ITEM (ethi)->canvas); + + /* We might be invoked before we are realized */ + if (!canvas->window) + return; + + if (is_pointer_on_division (ethi, pos, NULL, NULL)) + e_cursor_set (canvas->window, E_CURSOR_SIZE_X); + else + e_cursor_set (canvas->window, E_CURSOR_ARROW); +} + +static void +ethi_request_redraw (ETableHeaderItem *ethi) +{ + GnomeCanvas *canvas = GNOME_CANVAS_ITEM (ethi)->canvas; + + /* + * request a redraw + */ + gnome_canvas_request_redraw ( + canvas, ethi->x1, ethi->y1, INT_MAX, ethi->x1 + ethi->height); +} + +static void +ethi_end_resize (ETableHeaderItem *ethi, int new_size) +{ + e_table_header_set_size (ethi->eth, ethi->resize_col, new_size); + + if (ethi->resize_guide){ +#warning Fix this + /* gtk_object_destroy (ethi->resize_guide);*/ + ethi->resize_guide = NULL; + } + ethi->resize_col = -1; + ethi_request_redraw (ethi); +} + +/* + * Handles the events on the ETableHeaderItem, particularly it handles resizing + */ static int ethi_event (GnomeCanvasItem *item, GdkEvent *e) { + ETableHeaderItem *ethi = E_TABLE_HEADER_ITEM (item); + GnomeCanvas *canvas = item->canvas; + const gboolean resizing = ETHI_RESIZING (ethi); + int x, y, start, col; + switch (e->type){ + case GDK_ENTER_NOTIFY: + convert (canvas, e->crossing.x, e->crossing.y, &x, &y); + set_cursor (ethi, x); + break; + + case GDK_MOTION_NOTIFY: + convert (canvas, e->motion.x, e->motion.y, &x, &y); + if (resizing){ + if (ethi->resize_guide == NULL){ + /* Quick hack until I actually bind the views */ + ethi->resize_guide = GINT_TO_POINTER (1); + gnome_canvas_item_grab (item, + GDK_POINTER_MOTION_MASK | + GDK_BUTTON_RELEASE_MASK, + e_cursor_get (E_CURSOR_SIZE_X), + e->button.time); + } + + if (x - ethi->resize_start_pos <= 0) + break; + + ethi->resize_width = x - ethi->resize_start_pos; + + ethi_request_redraw (ethi); + } else + set_cursor (ethi, x); + break; + + case GDK_BUTTON_PRESS: + convert (canvas, e->button.x, e->button.y, &x, &y); + + if (is_pointer_on_division (ethi, x, &start, &col)){ + ETableCol *ecol; + + /* + * Record the important bits. + * + * By setting resize_pos to a non -1 value, + * we know that we are being resized (used in the + * other event handlers). + */ + ecol = e_table_header_get_column (ethi->eth, col); + ethi->resize_col = col; + ethi->resize_width = ecol->width; + ethi->resize_start_pos = start - ecol->width; + } + break; + + case GDK_2BUTTON_PRESS: + if (!resizing) + break; + + if (e->button.button != 1) + break; + + printf ("Resize this guy\n"); + break; + + case GDK_BUTTON_RELEASE: { + gboolean needs_ungrab = FALSE; + + if (ethi->resize_col != -1){ + needs_ungrab = (ethi->resize_guide != NULL); + ethi_end_resize (ethi, ethi->resize_width); + } + if (needs_ungrab) + gnome_canvas_item_ungrab (item, e->button.time); + + break; + } + default: return FALSE; } @@ -179,7 +402,7 @@ ethi_class_init (GtkObjectClass *object_class) { GnomeCanvasItemClass *item_class = (GnomeCanvasItemClass *) object_class; - ethi_parent_class = gtk_type_class (gnome_canvas_item_get_type ()); + ethi_parent_class = gtk_type_class (PARENT_OBJECT_TYPE); object_class->destroy = ethi_destroy; object_class->set_arg = ethi_set_arg; @@ -193,11 +416,21 @@ ethi_class_init (GtkObjectClass *object_class) gtk_object_add_arg_type ("ETableHeaderItem::ETableHeader", GTK_TYPE_POINTER, GTK_ARG_WRITABLE, ARG_TABLE_HEADER); + gtk_object_add_arg_type ("ETableHeaderItem::x", GTK_TYPE_INT, + GTK_ARG_WRITABLE, ARG_TABLE_X); + gtk_object_add_arg_type ("ETableHeaderItem::y", GTK_TYPE_INT, + GTK_ARG_WRITABLE, ARG_TABLE_Y); + gtk_object_add_arg_type ("ETableHeaderItem::fontset", GTK_TYPE_STRING, + GTK_ARG_WRITABLE, ARG_TABLE_FONTSET); } static void ethi_init (GnomeCanvasItem *item) { + ETableHeaderItem *ethi = E_TABLE_HEADER_ITEM (item); + + ethi->resize_col = -1; + item->x1 = 0; item->y1 = 0; item->x2 = 0; diff --git a/widgets/table/e-table-header-item.h b/widgets/table/e-table-header-item.h index 9a66b56782..45e8f8851d 100644 --- a/widgets/table/e-table-header-item.h +++ b/widgets/table/e-table-header-item.h @@ -16,6 +16,17 @@ typedef struct { GdkGC *gc; GdkCursor *change_cursor, *normal_cursor; + + short x1, y1, height; + GdkFont *font; + + /* + * Used during resizing + */ + int resize_col; + int resize_width; + int resize_start_pos; + GtkObject *resize_guide; } ETableHeaderItem; typedef struct { diff --git a/widgets/table/e-table-header.c b/widgets/table/e-table-header.c index 24ae2fdd23..3edc57a527 100644 --- a/widgets/table/e-table-header.c +++ b/widgets/table/e-table-header.c @@ -314,4 +314,3 @@ e_table_header_set_size (ETableHeader *eth, int idx, int size) eth->columns [idx]->width = size; gtk_signal_emit (GTK_OBJECT (eth), eth_signals [DIMENSION_CHANGE], idx); } - diff --git a/widgets/table/e-table-header.h b/widgets/table/e-table-header.h index 89eced0158..c263c36ad3 100644 --- a/widgets/table/e-table-header.h +++ b/widgets/table/e-table-header.h @@ -59,3 +59,4 @@ GList *e_table_header_get_selected_indexes(ETableHeader *eth); #endif /* _E_TABLE_HEADER_H_ */ + diff --git a/widgets/table/e-table-model.c b/widgets/table/e-table-model.c index 646aee77bd..c3b6674130 100644 --- a/widgets/table/e-table-model.c +++ b/widgets/table/e-table-model.c @@ -113,3 +113,23 @@ e_table_model_height (ETableModel *etable) return size; } +#if 0 +int +e_table_model_max_col_width (ETableModel *etm, int col) +{ + const int nvals = e_table_model_row_count (etm); + int max = 0; + int row; + + for (row = 0; row < nvals; row++){ + int w; + + w = e_table_model_cell_width (etm, col, i); + + if (w > max) + max = w; + } + + return max; +} +#endif diff --git a/widgets/table/e-table-model.h b/widgets/table/e-table-model.h index e32d3d55ce..ce57f73e7a 100644 --- a/widgets/table/e-table-model.h +++ b/widgets/table/e-table-model.h @@ -44,3 +44,4 @@ gboolean e_table_model_is_cell_editable (ETableModel *e_table_model, int col, */ #endif /* _E_TABLE_MODEL_H_ */ + diff --git a/widgets/table/e-table-sorted.c b/widgets/table/e-table-sorted.c new file mode 100644 index 0000000000..d8c5273800 --- /dev/null +++ b/widgets/table/e-table-sorted.c @@ -0,0 +1,90 @@ +/* + * E-table-sorted.c: Implements a table that sorts another table + * + * Author: + * Miguel de Icaza (miguel@gnu.org) + * + * (C) 1999 Helix Code, Inc. + */ +#include +#include +#include "e-util.h" +#include "e-table-sorted.h" + +#define PARENT_TYPE E_TABLE_MODEL_TYPE + +static ETableModelClass *ets_parent_class; + +static void +ets_destroy (GtkObject *object) +{ + ETableSorted *ets = E_TABLE_SORTED (object); + + gtk_object_unref (GTK_OBJECT (ets->source)); + gtk_object_unref (GTK_OBJECT (ets->header)); + free (ets->map_table); + + GTK_OBJECT_CLASS (ets_parent_class)->destroy (object); +} + +static void +ets_class_init (GtkObjectClass *klass) +{ + ets_parent_class = gtk_type_class (PARENT_TYPE); + klass->destroy = ets_destroy; +} + +E_MAKE_TYPE(e_table_sorted, "ETableSorted", ETableSorted, ets_class_init, NULL, PARENT_TYPE); + +static ETableSorted *sort_ets; + +static int +my_sort (const void *a, const void *b) +{ + GCompareFunc comp; + const int *ia = (const int *) a; + const int *ib = (const int *) b; + void *va, *vb; + + va = e_table_model_value_at (sort_ets->source, sort_ets->sort_idx, *ia); + vb = e_table_model_value_at (sort_ets->source, sort_ets->sort_idx, *ib); + + comp = sort_ets->sort_col->compare; + + return (*comp) (va, vb); +} + +ETableModel * +e_table_sorted_new (ETableModel *source, ETableHeader *header, short sort_field) +{ + ETableSorted *ets = gtk_type_new (E_TABLE_SORTED_TYPE); + const int nvals = e_table_model_row_count (source); + unsigned int *buffer; + int i; + + buffer = malloc (sizeof (unsigned int *) * nvals); + if (buffer = NULL) + return NULL; + ets->map_table = buffer; + ets->n_map = nvals; + ets->source = source; + ets->header = header; + ets->sort_col = e_table_header_get_column (header, sort_field); + ets->sort_idx = sort_field; + gtk_object_ref (GTK_OBJECT (source)); + gtk_object_ref (GTK_OBJECT (header)); + + /* Init */ + for (i = 0; i < nvals; i++) + ets->map_table [i] = i; + + /* Sort */ + g_assert (sort_ets == NULL); + sort_ets = ets; + qsort (ets->map_table, nvals, sizeof (unsigned int), my_sort); + sort_ets = NULL; + + return (ETableModel *) ets; +} + + diff --git a/widgets/table/e-table-sorted.h b/widgets/table/e-table-sorted.h new file mode 100644 index 0000000000..65578b7b83 --- /dev/null +++ b/widgets/table/e-table-sorted.h @@ -0,0 +1,34 @@ +#ifndef _E_TABLE_SORTED_H_ +#define _E_TABLE_SORTED_H_ + +#include +#include "e-table-model.h" +#include "e-table-header.h" + +#define E_TABLE_SORTED_TYPE (e_table_sorted_get_type ()) +#define E_TABLE_SORTED(o) (GTK_CHECK_CAST ((o), E_TABLE_SORTED_TYPE, ETableSorted)) +#define E_TABLE_SORTED_CLASS(k) (GTK_CHECK_CLASS_CAST((k), E_TABLE_SORTED_TYPE, ETableSortedClass)) +#define E_IS_TABLE_SORTED(o) (GTK_CHECK_TYPE ((o), E_TABLE_SORTED_TYPE)) +#define E_IS_TABLE_SORTED_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_TABLE_SORTED_TYPE)) + +typedef struct { + ETableModel base; + + ETableModel *source; + ETableHeader *header; + ETableCol *sort_col; + short sort_idx; + + int n_map; + unsigned int *map_table; +} ETableSorted; + +typedef struct { + ETableModelClass parent_class; +} ETableSortedClass; + +GtkType e_table_sorted_get_type (void); +ETableModel *e_table_sorted_new (ETableModel *etm, ETableHeader *header, + short sort_field); + +#endif /* _E_TABLE_SORTED_H_ */ diff --git a/widgets/table/table-test.c b/widgets/table/table-test.c index 27b1df7201..d67a211094 100644 --- a/widgets/table/table-test.c +++ b/widgets/table/table-test.c @@ -166,6 +166,12 @@ is_cell_editable (ETableModel *etc, int col, int row, void *data) return TRUE; } +static void +set_canvas_size (GnomeCanvas *canvas, GtkAllocation *alloc) +{ + gnome_canvas_set_scroll_region (canvas, 0, 0, alloc->width, alloc->height); +} + int main (int argc, char *argv []) { @@ -175,6 +181,7 @@ main (int argc, char *argv []) int i; gnome_init ("TableTest", "TableTest", argc, argv); + e_cursors_init (); load_data (); @@ -191,7 +198,7 @@ main (int argc, char *argv []) e_table_header = e_table_header_new (); for (i = 0; i < cols; i++){ ETableCol *ecol = e_table_col_new ( - column_labels [i], 20, 20, e_table_render_string, + column_labels [i], 80, 20, e_table_render_string, NULL, g_str_equal, TRUE); e_table_header_add_column (e_table_header, ecol, i); @@ -203,14 +210,27 @@ main (int argc, char *argv []) window = gtk_window_new (GTK_WINDOW_TOPLEVEL); canvas = gnome_canvas_new (); + gtk_signal_connect (GTK_OBJECT (canvas), "size_allocate", + GTK_SIGNAL_FUNC (set_canvas_size), NULL); + gtk_container_add (GTK_CONTAINER (window), canvas); gtk_widget_show_all (window); - gnome_canvas_item_new ( gnome_canvas_root (GNOME_CANVAS (canvas)), e_table_header_item_get_type (), "ETableHeader", e_table_header, NULL); + gnome_canvas_item_new ( + gnome_canvas_root (GNOME_CANVAS (canvas)), + gnome_canvas_rect_get_type (), + "x1", 0.0, + "y1", 0.0, + "x2", 10.0, + "y2", 10.0, + "fill_color", "red", + NULL); gtk_main (); + + e_cursors_shutdown (); return 0; } -- cgit v1.2.3