From 78adc934b0946d1f4f4f6dacb393b4a119d68bec Mon Sep 17 00:00:00 2001 From: Christopher James Lahey Date: Sat, 5 Aug 2000 12:38:24 +0000 Subject: Made selection ranges work even if the table is sorted. 2000-08-05 Christopher James Lahey * Makefile.am, e-table-selection-model.c, e-table-selection-model.h, e-table.c, e-table.h: Made selection ranges work even if the table is sorted. * e-table-sorter.c, e-table-sorter.h: New files to help with making selection ranges work even if sorted. svn path=/trunk/; revision=4546 --- widgets/e-table/ChangeLog | 9 + widgets/e-table/Makefile.am | 2 + widgets/e-table/e-table-selection-model.c | 98 ++++++++-- widgets/e-table/e-table-selection-model.h | 6 +- widgets/e-table/e-table-sorter.c | 293 ++++++++++++++++++++++++++++++ widgets/e-table/e-table-sorter.h | 47 +++++ widgets/e-table/e-table.c | 12 +- widgets/e-table/e-table.h | 1 + widgets/table/e-table-selection-model.c | 98 ++++++++-- widgets/table/e-table-selection-model.h | 6 +- widgets/table/e-table-sorter.c | 293 ++++++++++++++++++++++++++++++ widgets/table/e-table-sorter.h | 47 +++++ widgets/table/e-table.c | 12 +- widgets/table/e-table.h | 1 + 14 files changed, 875 insertions(+), 50 deletions(-) create mode 100644 widgets/e-table/e-table-sorter.c create mode 100644 widgets/e-table/e-table-sorter.h create mode 100644 widgets/table/e-table-sorter.c create mode 100644 widgets/table/e-table-sorter.h (limited to 'widgets') diff --git a/widgets/e-table/ChangeLog b/widgets/e-table/ChangeLog index 4b91e9dcc2..29a3b3a588 100644 --- a/widgets/e-table/ChangeLog +++ b/widgets/e-table/ChangeLog @@ -1,3 +1,12 @@ +2000-08-05 Christopher James Lahey + + * Makefile.am, e-table-selection-model.c, + e-table-selection-model.h, e-table.c, e-table.h: Made selection + ranges work even if the table is sorted. + + * e-table-sorter.c, e-table-sorter.h: New files to help with + making selection ranges work even if sorted. + 2000-08-04 Christopher James Lahey * e-table.c, e-table.h: Built code to have autodrags only work diff --git a/widgets/e-table/Makefile.am b/widgets/e-table/Makefile.am index cd60b5e7a2..30ee6f3d7d 100644 --- a/widgets/e-table/Makefile.am +++ b/widgets/e-table/Makefile.am @@ -78,6 +78,8 @@ libetable_a_SOURCES = \ e-table-sorted.h \ e-table-sorted-variable.c \ e-table-sorted-variable.h \ + e-table-sorter.c \ + e-table-sorter.h \ e-table-subset.c \ e-table-subset.h \ e-table-subset-variable.c \ diff --git a/widgets/e-table/e-table-selection-model.c b/widgets/e-table/e-table-selection-model.c index 9bd3cb38ac..a40298c992 100644 --- a/widgets/e-table/e-table-selection-model.c +++ b/widgets/e-table/e-table-selection-model.c @@ -20,9 +20,9 @@ #define BOX(n) ((n) / 32) #define OFFSET(n) (31 - ((n) % 32)) -#define BITMASK(n) (((guint32) 0x1) << OFFSET(n)) -#define BITMASK_LEFT(n) (((guint32) ONES) << (32 - ((n) % 32))) -#define BITMASK_RIGHT(n) (((guint32) ONES) >> ((n) % 32)) +#define BITMASK(n) ((guint32)(((guint32) 0x1) << OFFSET(n))) +#define BITMASK_LEFT(n) ((guint32)(((guint32) ONES) << (32 - ((n) % 32)))) +#define BITMASK_RIGHT(n) ((guint32)(((guint32) ONES) >> ((n) % 32))) static GtkObjectClass *e_table_selection_model_parent_class; @@ -37,6 +37,7 @@ static guint e_table_selection_model_signals [LAST_SIGNAL] = { 0, }; enum { ARG_0, ARG_MODEL, + ARG_SORTER, ARG_CURSOR_ROW, ARG_CURSOR_COL, }; @@ -151,6 +152,24 @@ drop_model(ETableSelectionModel *etsm) etsm->model = NULL; } +inline static void +add_sorter(ETableSelectionModel *etsm, ETableSorter *sorter) +{ + etsm->sorter = sorter; + if (sorter) { + gtk_object_ref(GTK_OBJECT(sorter)); + } +} + +inline static void +drop_sorter(ETableSelectionModel *etsm) +{ + if (etsm->sorter) { + gtk_object_unref(GTK_OBJECT(etsm->sorter)); + } + etsm->sorter = NULL; +} + static void etsm_destroy (GtkObject *object) { @@ -159,6 +178,7 @@ etsm_destroy (GtkObject *object) etsm = E_TABLE_SELECTION_MODEL (object); drop_model(etsm); + drop_sorter(etsm); g_free(etsm->selection); } @@ -173,6 +193,10 @@ etsm_get_arg (GtkObject *o, GtkArg *arg, guint arg_id) GTK_VALUE_OBJECT (*arg) = GTK_OBJECT(etsm->model); break; + case ARG_SORTER: + GTK_VALUE_OBJECT (*arg) = GTK_OBJECT(etsm->sorter); + break; + case ARG_CURSOR_ROW: GTK_VALUE_INT(*arg) = etsm->cursor_row; break; @@ -194,6 +218,11 @@ etsm_set_arg (GtkObject *o, GtkArg *arg, guint arg_id) add_model(etsm, GTK_VALUE_OBJECT (*arg) ? E_TABLE_MODEL(GTK_VALUE_OBJECT (*arg)) : NULL); break; + case ARG_SORTER: + drop_sorter(etsm); + add_sorter(etsm, GTK_VALUE_OBJECT (*arg) ? E_TABLE_SORTER(GTK_VALUE_OBJECT (*arg)) : NULL); + break; + case ARG_CURSOR_ROW: e_table_selection_model_do_something(etsm, GTK_VALUE_INT(*arg), etsm->cursor_col, 0); break; @@ -248,6 +277,8 @@ e_table_selection_model_class_init (ETableSelectionModelClass *klass) gtk_object_add_arg_type ("ETableSelectionModel::model", GTK_TYPE_OBJECT, GTK_ARG_READWRITE, ARG_MODEL); + gtk_object_add_arg_type ("ETableSelectionModel::sorter", GTK_TYPE_OBJECT, + GTK_ARG_READWRITE, ARG_SORTER); gtk_object_add_arg_type ("ETableSelectionModel::cursor_row", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_CURSOR_ROW); gtk_object_add_arg_type ("ETableSelectionModel::cursor_col", GTK_TYPE_INT, @@ -294,26 +325,42 @@ e_table_selection_model_foreach (ETableSelectionModel *selection, } } -#define OPERATE(object, mask, grow) ((grow) ? ((object) |= ~(mask)) : ((object) &= (mask))) +#define OPERATE(object, mask, grow) ((grow) ? ((object) |= (~(mask))) : ((object) &= (mask))) + +static void +change_one_row(ETableSelectionModel *selection, int row, gboolean grow) +{ + int i; + i = BOX(row); + + OPERATE(selection->selection[i], BITMASK_LEFT(row) | BITMASK_RIGHT(row + 1), grow); +} static void change_selection(ETableSelectionModel *selection, int start, int end, gboolean grow) { int i, last; if (start != end) { - i = BOX(start); - last = BOX(end); - - if (i == last) { - OPERATE(selection->selection[i], BITMASK_LEFT(start) | BITMASK_RIGHT(end), grow); + if (selection->sorter && e_table_sorter_needs_sorting(selection->sorter)) { + for ( i = start; i < end; i++) { + change_one_row(selection, e_table_sorter_sorted_to_model(selection->sorter, i), grow); + } } else { - OPERATE(selection->selection[i], BITMASK_LEFT(start), grow); - if (grow) - for (i ++; i < last; i++) - selection->selection[i] = ONES; - for (i ++; i < last; i++) - selection->selection[i] = 0; - OPERATE(selection->selection[i], BITMASK_RIGHT(end), grow); + i = BOX(start); + last = BOX(end); + + if (i == last) { + OPERATE(selection->selection[i], BITMASK_LEFT(start) | BITMASK_RIGHT(end), grow); + } else { + OPERATE(selection->selection[i], BITMASK_LEFT(start), grow); + if (grow) + for (i ++; i < last; i++) + selection->selection[i] = ONES; + else + for (i ++; i < last; i++) + selection->selection[i] = 0; + OPERATE(selection->selection[i], BITMASK_RIGHT(end), grow); + } } } } @@ -338,10 +385,21 @@ void e_table_selection_model_do_something (ETableSelectionModel int old_end; int new_start; int new_end; - old_start = MIN (selection->selection_start_row, selection->cursor_row); - old_end = MAX (selection->selection_start_row, selection->cursor_row) + 1; - new_start = MIN (selection->selection_start_row, row); - new_end = MAX (selection->selection_start_row, row) + 1; + if (selection->sorter && e_table_sorter_needs_sorting(selection->sorter)) { + old_start = MIN (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, selection->cursor_row)); + old_end = MAX (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, selection->cursor_row)) + 1; + new_start = MIN (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, row)); + new_end = MAX (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, row)) + 1; + } else { + old_start = MIN (selection->selection_start_row, selection->cursor_row); + old_end = MAX (selection->selection_start_row, selection->cursor_row) + 1; + new_start = MIN (selection->selection_start_row, row); + new_end = MAX (selection->selection_start_row, row) + 1; + } /* This wouldn't work nearly so smoothly if one end of the selection held in place. */ if (old_start < new_start) change_selection(selection, old_start, new_start, FALSE); diff --git a/widgets/e-table/e-table-selection-model.h b/widgets/e-table/e-table-selection-model.h index a1ccb41353..df2e377d14 100644 --- a/widgets/e-table/e-table-selection-model.h +++ b/widgets/e-table/e-table-selection-model.h @@ -5,6 +5,7 @@ #include #include "widgets/e-table/e-table-model.h" #include "widgets/e-table/e-table-defines.h" +#include "widgets/e-table/e-table-sorter.h" #define E_TABLE_SELECTION_MODEL_TYPE (e_table_selection_model_get_type ()) #define E_TABLE_SELECTION_MODEL(o) (GTK_CHECK_CAST ((o), E_TABLE_SELECTION_MODEL_TYPE, ETableSelectionModel)) @@ -13,9 +14,10 @@ #define E_IS_TABLE_SELECTION_MODEL_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_TABLE_SELECTION_MODEL_TYPE)) typedef struct { - GtkObject base; + GtkObject base; - ETableModel *model; + ETableModel *model; + ETableSorter *sorter; gint row_count; guint32 *selection; diff --git a/widgets/e-table/e-table-sorter.c b/widgets/e-table/e-table-sorter.c new file mode 100644 index 0000000000..6da1c01e76 --- /dev/null +++ b/widgets/e-table/e-table-sorter.c @@ -0,0 +1,293 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * 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 +#include +#include "e-util/e-util.h" +#include "e-table-sorter.h" + +#define PARENT_TYPE gtk_object_get_type() + +#define INCREMENT_AMOUNT 100 + +static GtkObjectClass *parent_class; + +static void ets_model_changed (ETableModel *etm, ETableSorter *ets); +static void ets_model_row_changed (ETableModel *etm, int row, ETableSorter *ets); +static void ets_model_cell_changed (ETableModel *etm, int col, int row, ETableSorter *ets); +static void ets_sort_info_changed (ETableSortInfo *info, ETableSorter *ets); +static void ets_clean (ETableSorter *ets); +static void ets_sort (ETableSorter *ets); +static void ets_backsort (ETableSorter *ets); + +static void +ets_destroy (GtkObject *object) +{ + ETableSorter *ets = E_TABLE_SORTER (object); + + gtk_signal_disconnect (GTK_OBJECT (ets->source), + ets->table_model_changed_id); + gtk_signal_disconnect (GTK_OBJECT (ets->source), + ets->table_model_row_changed_id); + gtk_signal_disconnect (GTK_OBJECT (ets->source), + ets->table_model_cell_changed_id); + gtk_signal_disconnect (GTK_OBJECT (ets->sort_info), + ets->sort_info_changed_id); + + ets->table_model_changed_id = 0; + ets->table_model_row_changed_id = 0; + ets->table_model_cell_changed_id = 0; + ets->sort_info_changed_id = 0; + + if (ets->sort_info) + gtk_object_unref(GTK_OBJECT(ets->sort_info)); + if (ets->full_header) + gtk_object_unref(GTK_OBJECT(ets->full_header)); + if (ets->source) + gtk_object_unref(GTK_OBJECT(ets->source)); + + GTK_OBJECT_CLASS (parent_class)->destroy (object); +} + +static void +ets_class_init (ETableSorterClass *klass) +{ + GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass); + + parent_class = gtk_type_class (PARENT_TYPE); + + object_class->destroy = ets_destroy; +} + +static void +ets_init (ETableSorter *ets) +{ + ets->full_header = NULL; + ets->sort_info = NULL; + ets->source = NULL; + + ets->needs_sorting = -1; + + ets->table_model_changed_id = 0; + ets->table_model_row_changed_id = 0; + ets->table_model_cell_changed_id = 0; + ets->sort_info_changed_id = 0; +} + +E_MAKE_TYPE(e_table_sorter, "ETableSorter", ETableSorter, ets_class_init, ets_init, PARENT_TYPE); + +ETableSorter * +e_table_sorter_new (ETableModel *source, ETableHeader *full_header, ETableSortInfo *sort_info) +{ + ETableSorter *ets = gtk_type_new (E_TABLE_SORTER_TYPE); + + ets->sort_info = sort_info; + gtk_object_ref(GTK_OBJECT(ets->sort_info)); + ets->full_header = full_header; + gtk_object_ref(GTK_OBJECT(ets->full_header)); + ets->source = source; + gtk_object_ref(GTK_OBJECT(ets->source)); + + ets->table_model_changed_id = gtk_signal_connect (GTK_OBJECT (source), "model_changed", + GTK_SIGNAL_FUNC (ets_model_changed), ets); + ets->table_model_row_changed_id = gtk_signal_connect (GTK_OBJECT (source), "model_row_changed", + GTK_SIGNAL_FUNC (ets_model_row_changed), ets); + ets->table_model_cell_changed_id = gtk_signal_connect (GTK_OBJECT (source), "model_cell_changed", + GTK_SIGNAL_FUNC (ets_model_cell_changed), ets); + ets->sort_info_changed_id = gtk_signal_connect (GTK_OBJECT (sort_info), "sort_info_changed", + GTK_SIGNAL_FUNC (ets_sort_info_changed), ets); + + return ets; +} + +static void +ets_model_changed (ETableModel *etm, ETableSorter *ets) +{ + ets_clean(ets); +} + +static void +ets_model_row_changed (ETableModel *etm, int row, ETableSorter *ets) +{ + ets_clean(ets); +} + +static void +ets_model_cell_changed (ETableModel *etm, int col, int row, ETableSorter *ets) +{ + ets_clean(ets); +} + +static void +ets_sort_info_changed (ETableSortInfo *info, ETableSorter *ets) +{ + ets_clean(ets); +} + +static ETableSorter *ets_closure; +void **vals_closure; +int cols_closure; +int *ascending_closure; +GCompareFunc *compare_closure; + +/* FIXME: Make it not cache the second and later columns (as if anyone cares.) */ + +static int +qsort_callback(const void *data1, const void *data2) +{ + gint row1 = *(int *)data1; + gint row2 = *(int *)data2; + int j; + int sort_count = e_table_sort_info_sorting_get_count(ets_closure->sort_info) + e_table_sort_info_grouping_get_count(ets_closure->sort_info); + int comp_val = 0; + int ascending = 1; + for (j = 0; j < sort_count; j++) { + comp_val = (*(compare_closure[j]))(vals_closure[cols_closure * row1 + j], vals_closure[cols_closure * row2 + j]); + ascending = ascending_closure[j]; + if (comp_val != 0) + break; + } + if (comp_val == 0) { + if (row1 < row2) + comp_val = -1; + if (row1 > row2) + comp_val = 1; + } + if (!ascending) + comp_val = -comp_val; + return comp_val; +} + +static void +ets_clean(ETableSorter *ets) +{ + g_free(ets->sorted); + ets->sorted = NULL; + + g_free(ets->backsorted); + ets->backsorted = NULL; + + ets->needs_sorting = -1; +} + +static void +ets_sort(ETableSorter *ets) +{ + int rows; + int i; + int j; + int cols; + + if (ets->sorted) + return; + + rows = e_table_model_row_count(ets->source); + cols = e_table_sort_info_sorting_get_count(ets->sort_info) + e_table_sort_info_grouping_get_count(ets->sort_info); + + ets->sorted = g_new(int, rows); + for (i = 0; i < rows; i++) + ets->sorted[i] = i; + + cols_closure = cols; + ets_closure = ets; + + vals_closure = g_new(void *, rows * cols); + ascending_closure = g_new(int, cols); + compare_closure = g_new(GCompareFunc, cols); + + for (j = 0; j < cols; j++) { + ETableSortColumn column; + ETableCol *col; + + if (j < e_table_sort_info_sorting_get_count(ets->sort_info)) + column = e_table_sort_info_sorting_get_nth(ets->sort_info, j); + else + column = e_table_sort_info_grouping_get_nth(ets->sort_info, j); + + if (column.column > e_table_header_count (ets->full_header)) + col = e_table_header_get_column (ets->full_header, e_table_header_count (ets->full_header) - 1); + else + col = e_table_header_get_column (ets->full_header, column.column); + + for (i = 0; i < rows; i++) { + vals_closure[i * cols + j] = e_table_model_value_at (ets->source, col->col_idx, i); + } + + compare_closure[j] = col->compare; + ascending_closure[j] = column.ascending; + } + qsort(ets->sorted, rows, sizeof(int), qsort_callback); + + g_free(vals_closure); + g_free(ascending_closure); + g_free(compare_closure); +} + +static void +ets_backsort(ETableSorter *ets) +{ + int i, rows; + + if (ets->backsorted) + return; + + ets_sort(ets); + + rows = e_table_model_row_count(ets->source); + ets->backsorted = g_new0(int, rows); + + for (i = 0; i < rows; i++) { + ets->backsorted[ets->sorted[i]] = i; + } +} + +gboolean +e_table_sorter_needs_sorting(ETableSorter *ets) +{ + if (ets->needs_sorting < 0) { + if (e_table_sort_info_sorting_get_count(ets->sort_info) + e_table_sort_info_grouping_get_count(ets->sort_info)) + ets->needs_sorting = 1; + else + ets->needs_sorting = 0; + } + return ets->needs_sorting; +} + + +gint +e_table_sorter_model_to_sorted (ETableSorter *sorter, int row) +{ + int rows = e_table_model_row_count(sorter->source); + + g_return_val_if_fail(row >= 0, -1); + g_return_val_if_fail(row < rows, -1); + + if (e_table_sorter_needs_sorting(sorter)) { + ets_backsort(sorter); + return sorter->backsorted[row]; + } else + return row; +} + +gint +e_table_sorter_sorted_to_model (ETableSorter *sorter, int row) +{ + int rows = e_table_model_row_count(sorter->source); + + g_return_val_if_fail(row >= 0, -1); + g_return_val_if_fail(row < rows, -1); + + if (e_table_sorter_needs_sorting(sorter)) { + ets_sort(sorter); + return sorter->sorted[row]; + } else + return row; +} diff --git a/widgets/e-table/e-table-sorter.h b/widgets/e-table/e-table-sorter.h new file mode 100644 index 0000000000..f5a71565a3 --- /dev/null +++ b/widgets/e-table/e-table-sorter.h @@ -0,0 +1,47 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +#ifndef _E_TABLE_SORTER_H_ +#define _E_TABLE_SORTER_H_ + +#include +#include "e-table-model.h" +#include "e-table-subset-variable.h" +#include "e-table-sort-info.h" +#include "e-table-header.h" + +#define E_TABLE_SORTER_TYPE (e_table_sorter_get_type ()) +#define E_TABLE_SORTER(o) (GTK_CHECK_CAST ((o), E_TABLE_SORTER_TYPE, ETableSorter)) +#define E_TABLE_SORTER_CLASS(k) (GTK_CHECK_CLASS_CAST((k), E_TABLE_SORTER_TYPE, ETableSorterClass)) +#define E_IS_TABLE_SORTER(o) (GTK_CHECK_TYPE ((o), E_TABLE_SORTER_TYPE)) +#define E_IS_TABLE_SORTER_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_TABLE_SORTER_TYPE)) + +typedef struct { + GtkObject base; + + ETableModel *source; + ETableHeader *full_header; + ETableSortInfo *sort_info; + + int needs_sorting; + + int *sorted; + int *backsorted; + + int table_model_changed_id; + int table_model_row_changed_id; + int table_model_cell_changed_id; + int sort_info_changed_id; +} ETableSorter; + +typedef struct { + GtkObjectClass parent_class; +} ETableSorterClass; + +GtkType e_table_sorter_get_type (void); +ETableSorter *e_table_sorter_new (ETableModel *etm, ETableHeader *full_header, ETableSortInfo *sort_info); + +gint e_table_sorter_model_to_sorted (ETableSorter *sorter, int row); +gint e_table_sorter_sorted_to_model (ETableSorter *sorter, int row); + +gboolean e_table_sorter_needs_sorting (ETableSorter *sorter); + +#endif /* _E_TABLE_SORTER_H_ */ diff --git a/widgets/e-table/e-table.c b/widgets/e-table/e-table.c index 4daddc3985..c6fffffd3d 100644 --- a/widgets/e-table/e-table.c +++ b/widgets/e-table/e-table.c @@ -144,6 +144,7 @@ et_destroy (GtkObject *object) gtk_object_unref (GTK_OBJECT (et->full_header)); gtk_object_unref (GTK_OBJECT (et->header)); gtk_object_unref (GTK_OBJECT (et->sort_info)); + gtk_object_unref (GTK_OBJECT (et->sorter)); gtk_widget_destroy (GTK_WIDGET (et->header_canvas)); gtk_widget_destroy (GTK_WIDGET (et->table_canvas)); @@ -187,6 +188,7 @@ e_table_init (GtkObject *object) e_table->drag_source_button_press_event_id = 0; e_table->drag_source_motion_notify_event_id = 0; + e_table->sorter = NULL; e_table->selection = e_table_selection_model_new(); e_table->cursor_loc = E_TABLE_CURSOR_LOC_NONE; } @@ -603,9 +605,6 @@ et_real_construct (ETable *e_table, ETableHeader *full_header, ETableModel *etm, e_table->model = etm; gtk_object_ref (GTK_OBJECT (etm)); - gtk_object_set (GTK_OBJECT (e_table->selection), - "model", etm, - NULL); gtk_widget_push_visual (gdk_rgb_get_visual ()); gtk_widget_push_colormap (gdk_rgb_get_cmap ()); @@ -617,6 +616,13 @@ et_real_construct (ETable *e_table, ETableHeader *full_header, ETableModel *etm, "sort_info", e_table->sort_info, NULL); + e_table->sorter = e_table_sorter_new(etm, e_table->full_header, e_table->sort_info); + + gtk_object_set (GTK_OBJECT (e_table->selection), + "model", etm, + "sorter", e_table->sorter, + NULL); + if (!no_header) { e_table_setup_header (e_table); } diff --git a/widgets/e-table/e-table.h b/widgets/e-table/e-table.h index 6f4fe153c6..a418a284a6 100644 --- a/widgets/e-table/e-table.h +++ b/widgets/e-table/e-table.h @@ -40,6 +40,7 @@ typedef struct { ETableGroup *group; ETableSortInfo *sort_info; + ETableSorter *sorter; ETableSelectionModel *selection; ETableCursorLoc cursor_loc; diff --git a/widgets/table/e-table-selection-model.c b/widgets/table/e-table-selection-model.c index 9bd3cb38ac..a40298c992 100644 --- a/widgets/table/e-table-selection-model.c +++ b/widgets/table/e-table-selection-model.c @@ -20,9 +20,9 @@ #define BOX(n) ((n) / 32) #define OFFSET(n) (31 - ((n) % 32)) -#define BITMASK(n) (((guint32) 0x1) << OFFSET(n)) -#define BITMASK_LEFT(n) (((guint32) ONES) << (32 - ((n) % 32))) -#define BITMASK_RIGHT(n) (((guint32) ONES) >> ((n) % 32)) +#define BITMASK(n) ((guint32)(((guint32) 0x1) << OFFSET(n))) +#define BITMASK_LEFT(n) ((guint32)(((guint32) ONES) << (32 - ((n) % 32)))) +#define BITMASK_RIGHT(n) ((guint32)(((guint32) ONES) >> ((n) % 32))) static GtkObjectClass *e_table_selection_model_parent_class; @@ -37,6 +37,7 @@ static guint e_table_selection_model_signals [LAST_SIGNAL] = { 0, }; enum { ARG_0, ARG_MODEL, + ARG_SORTER, ARG_CURSOR_ROW, ARG_CURSOR_COL, }; @@ -151,6 +152,24 @@ drop_model(ETableSelectionModel *etsm) etsm->model = NULL; } +inline static void +add_sorter(ETableSelectionModel *etsm, ETableSorter *sorter) +{ + etsm->sorter = sorter; + if (sorter) { + gtk_object_ref(GTK_OBJECT(sorter)); + } +} + +inline static void +drop_sorter(ETableSelectionModel *etsm) +{ + if (etsm->sorter) { + gtk_object_unref(GTK_OBJECT(etsm->sorter)); + } + etsm->sorter = NULL; +} + static void etsm_destroy (GtkObject *object) { @@ -159,6 +178,7 @@ etsm_destroy (GtkObject *object) etsm = E_TABLE_SELECTION_MODEL (object); drop_model(etsm); + drop_sorter(etsm); g_free(etsm->selection); } @@ -173,6 +193,10 @@ etsm_get_arg (GtkObject *o, GtkArg *arg, guint arg_id) GTK_VALUE_OBJECT (*arg) = GTK_OBJECT(etsm->model); break; + case ARG_SORTER: + GTK_VALUE_OBJECT (*arg) = GTK_OBJECT(etsm->sorter); + break; + case ARG_CURSOR_ROW: GTK_VALUE_INT(*arg) = etsm->cursor_row; break; @@ -194,6 +218,11 @@ etsm_set_arg (GtkObject *o, GtkArg *arg, guint arg_id) add_model(etsm, GTK_VALUE_OBJECT (*arg) ? E_TABLE_MODEL(GTK_VALUE_OBJECT (*arg)) : NULL); break; + case ARG_SORTER: + drop_sorter(etsm); + add_sorter(etsm, GTK_VALUE_OBJECT (*arg) ? E_TABLE_SORTER(GTK_VALUE_OBJECT (*arg)) : NULL); + break; + case ARG_CURSOR_ROW: e_table_selection_model_do_something(etsm, GTK_VALUE_INT(*arg), etsm->cursor_col, 0); break; @@ -248,6 +277,8 @@ e_table_selection_model_class_init (ETableSelectionModelClass *klass) gtk_object_add_arg_type ("ETableSelectionModel::model", GTK_TYPE_OBJECT, GTK_ARG_READWRITE, ARG_MODEL); + gtk_object_add_arg_type ("ETableSelectionModel::sorter", GTK_TYPE_OBJECT, + GTK_ARG_READWRITE, ARG_SORTER); gtk_object_add_arg_type ("ETableSelectionModel::cursor_row", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_CURSOR_ROW); gtk_object_add_arg_type ("ETableSelectionModel::cursor_col", GTK_TYPE_INT, @@ -294,26 +325,42 @@ e_table_selection_model_foreach (ETableSelectionModel *selection, } } -#define OPERATE(object, mask, grow) ((grow) ? ((object) |= ~(mask)) : ((object) &= (mask))) +#define OPERATE(object, mask, grow) ((grow) ? ((object) |= (~(mask))) : ((object) &= (mask))) + +static void +change_one_row(ETableSelectionModel *selection, int row, gboolean grow) +{ + int i; + i = BOX(row); + + OPERATE(selection->selection[i], BITMASK_LEFT(row) | BITMASK_RIGHT(row + 1), grow); +} static void change_selection(ETableSelectionModel *selection, int start, int end, gboolean grow) { int i, last; if (start != end) { - i = BOX(start); - last = BOX(end); - - if (i == last) { - OPERATE(selection->selection[i], BITMASK_LEFT(start) | BITMASK_RIGHT(end), grow); + if (selection->sorter && e_table_sorter_needs_sorting(selection->sorter)) { + for ( i = start; i < end; i++) { + change_one_row(selection, e_table_sorter_sorted_to_model(selection->sorter, i), grow); + } } else { - OPERATE(selection->selection[i], BITMASK_LEFT(start), grow); - if (grow) - for (i ++; i < last; i++) - selection->selection[i] = ONES; - for (i ++; i < last; i++) - selection->selection[i] = 0; - OPERATE(selection->selection[i], BITMASK_RIGHT(end), grow); + i = BOX(start); + last = BOX(end); + + if (i == last) { + OPERATE(selection->selection[i], BITMASK_LEFT(start) | BITMASK_RIGHT(end), grow); + } else { + OPERATE(selection->selection[i], BITMASK_LEFT(start), grow); + if (grow) + for (i ++; i < last; i++) + selection->selection[i] = ONES; + else + for (i ++; i < last; i++) + selection->selection[i] = 0; + OPERATE(selection->selection[i], BITMASK_RIGHT(end), grow); + } } } } @@ -338,10 +385,21 @@ void e_table_selection_model_do_something (ETableSelectionModel int old_end; int new_start; int new_end; - old_start = MIN (selection->selection_start_row, selection->cursor_row); - old_end = MAX (selection->selection_start_row, selection->cursor_row) + 1; - new_start = MIN (selection->selection_start_row, row); - new_end = MAX (selection->selection_start_row, row) + 1; + if (selection->sorter && e_table_sorter_needs_sorting(selection->sorter)) { + old_start = MIN (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, selection->cursor_row)); + old_end = MAX (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, selection->cursor_row)) + 1; + new_start = MIN (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, row)); + new_end = MAX (e_table_sorter_model_to_sorted(selection->sorter, selection->selection_start_row), + e_table_sorter_model_to_sorted(selection->sorter, row)) + 1; + } else { + old_start = MIN (selection->selection_start_row, selection->cursor_row); + old_end = MAX (selection->selection_start_row, selection->cursor_row) + 1; + new_start = MIN (selection->selection_start_row, row); + new_end = MAX (selection->selection_start_row, row) + 1; + } /* This wouldn't work nearly so smoothly if one end of the selection held in place. */ if (old_start < new_start) change_selection(selection, old_start, new_start, FALSE); diff --git a/widgets/table/e-table-selection-model.h b/widgets/table/e-table-selection-model.h index a1ccb41353..df2e377d14 100644 --- a/widgets/table/e-table-selection-model.h +++ b/widgets/table/e-table-selection-model.h @@ -5,6 +5,7 @@ #include #include "widgets/e-table/e-table-model.h" #include "widgets/e-table/e-table-defines.h" +#include "widgets/e-table/e-table-sorter.h" #define E_TABLE_SELECTION_MODEL_TYPE (e_table_selection_model_get_type ()) #define E_TABLE_SELECTION_MODEL(o) (GTK_CHECK_CAST ((o), E_TABLE_SELECTION_MODEL_TYPE, ETableSelectionModel)) @@ -13,9 +14,10 @@ #define E_IS_TABLE_SELECTION_MODEL_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_TABLE_SELECTION_MODEL_TYPE)) typedef struct { - GtkObject base; + GtkObject base; - ETableModel *model; + ETableModel *model; + ETableSorter *sorter; gint row_count; guint32 *selection; diff --git a/widgets/table/e-table-sorter.c b/widgets/table/e-table-sorter.c new file mode 100644 index 0000000000..6da1c01e76 --- /dev/null +++ b/widgets/table/e-table-sorter.c @@ -0,0 +1,293 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * 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 +#include +#include "e-util/e-util.h" +#include "e-table-sorter.h" + +#define PARENT_TYPE gtk_object_get_type() + +#define INCREMENT_AMOUNT 100 + +static GtkObjectClass *parent_class; + +static void ets_model_changed (ETableModel *etm, ETableSorter *ets); +static void ets_model_row_changed (ETableModel *etm, int row, ETableSorter *ets); +static void ets_model_cell_changed (ETableModel *etm, int col, int row, ETableSorter *ets); +static void ets_sort_info_changed (ETableSortInfo *info, ETableSorter *ets); +static void ets_clean (ETableSorter *ets); +static void ets_sort (ETableSorter *ets); +static void ets_backsort (ETableSorter *ets); + +static void +ets_destroy (GtkObject *object) +{ + ETableSorter *ets = E_TABLE_SORTER (object); + + gtk_signal_disconnect (GTK_OBJECT (ets->source), + ets->table_model_changed_id); + gtk_signal_disconnect (GTK_OBJECT (ets->source), + ets->table_model_row_changed_id); + gtk_signal_disconnect (GTK_OBJECT (ets->source), + ets->table_model_cell_changed_id); + gtk_signal_disconnect (GTK_OBJECT (ets->sort_info), + ets->sort_info_changed_id); + + ets->table_model_changed_id = 0; + ets->table_model_row_changed_id = 0; + ets->table_model_cell_changed_id = 0; + ets->sort_info_changed_id = 0; + + if (ets->sort_info) + gtk_object_unref(GTK_OBJECT(ets->sort_info)); + if (ets->full_header) + gtk_object_unref(GTK_OBJECT(ets->full_header)); + if (ets->source) + gtk_object_unref(GTK_OBJECT(ets->source)); + + GTK_OBJECT_CLASS (parent_class)->destroy (object); +} + +static void +ets_class_init (ETableSorterClass *klass) +{ + GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass); + + parent_class = gtk_type_class (PARENT_TYPE); + + object_class->destroy = ets_destroy; +} + +static void +ets_init (ETableSorter *ets) +{ + ets->full_header = NULL; + ets->sort_info = NULL; + ets->source = NULL; + + ets->needs_sorting = -1; + + ets->table_model_changed_id = 0; + ets->table_model_row_changed_id = 0; + ets->table_model_cell_changed_id = 0; + ets->sort_info_changed_id = 0; +} + +E_MAKE_TYPE(e_table_sorter, "ETableSorter", ETableSorter, ets_class_init, ets_init, PARENT_TYPE); + +ETableSorter * +e_table_sorter_new (ETableModel *source, ETableHeader *full_header, ETableSortInfo *sort_info) +{ + ETableSorter *ets = gtk_type_new (E_TABLE_SORTER_TYPE); + + ets->sort_info = sort_info; + gtk_object_ref(GTK_OBJECT(ets->sort_info)); + ets->full_header = full_header; + gtk_object_ref(GTK_OBJECT(ets->full_header)); + ets->source = source; + gtk_object_ref(GTK_OBJECT(ets->source)); + + ets->table_model_changed_id = gtk_signal_connect (GTK_OBJECT (source), "model_changed", + GTK_SIGNAL_FUNC (ets_model_changed), ets); + ets->table_model_row_changed_id = gtk_signal_connect (GTK_OBJECT (source), "model_row_changed", + GTK_SIGNAL_FUNC (ets_model_row_changed), ets); + ets->table_model_cell_changed_id = gtk_signal_connect (GTK_OBJECT (source), "model_cell_changed", + GTK_SIGNAL_FUNC (ets_model_cell_changed), ets); + ets->sort_info_changed_id = gtk_signal_connect (GTK_OBJECT (sort_info), "sort_info_changed", + GTK_SIGNAL_FUNC (ets_sort_info_changed), ets); + + return ets; +} + +static void +ets_model_changed (ETableModel *etm, ETableSorter *ets) +{ + ets_clean(ets); +} + +static void +ets_model_row_changed (ETableModel *etm, int row, ETableSorter *ets) +{ + ets_clean(ets); +} + +static void +ets_model_cell_changed (ETableModel *etm, int col, int row, ETableSorter *ets) +{ + ets_clean(ets); +} + +static void +ets_sort_info_changed (ETableSortInfo *info, ETableSorter *ets) +{ + ets_clean(ets); +} + +static ETableSorter *ets_closure; +void **vals_closure; +int cols_closure; +int *ascending_closure; +GCompareFunc *compare_closure; + +/* FIXME: Make it not cache the second and later columns (as if anyone cares.) */ + +static int +qsort_callback(const void *data1, const void *data2) +{ + gint row1 = *(int *)data1; + gint row2 = *(int *)data2; + int j; + int sort_count = e_table_sort_info_sorting_get_count(ets_closure->sort_info) + e_table_sort_info_grouping_get_count(ets_closure->sort_info); + int comp_val = 0; + int ascending = 1; + for (j = 0; j < sort_count; j++) { + comp_val = (*(compare_closure[j]))(vals_closure[cols_closure * row1 + j], vals_closure[cols_closure * row2 + j]); + ascending = ascending_closure[j]; + if (comp_val != 0) + break; + } + if (comp_val == 0) { + if (row1 < row2) + comp_val = -1; + if (row1 > row2) + comp_val = 1; + } + if (!ascending) + comp_val = -comp_val; + return comp_val; +} + +static void +ets_clean(ETableSorter *ets) +{ + g_free(ets->sorted); + ets->sorted = NULL; + + g_free(ets->backsorted); + ets->backsorted = NULL; + + ets->needs_sorting = -1; +} + +static void +ets_sort(ETableSorter *ets) +{ + int rows; + int i; + int j; + int cols; + + if (ets->sorted) + return; + + rows = e_table_model_row_count(ets->source); + cols = e_table_sort_info_sorting_get_count(ets->sort_info) + e_table_sort_info_grouping_get_count(ets->sort_info); + + ets->sorted = g_new(int, rows); + for (i = 0; i < rows; i++) + ets->sorted[i] = i; + + cols_closure = cols; + ets_closure = ets; + + vals_closure = g_new(void *, rows * cols); + ascending_closure = g_new(int, cols); + compare_closure = g_new(GCompareFunc, cols); + + for (j = 0; j < cols; j++) { + ETableSortColumn column; + ETableCol *col; + + if (j < e_table_sort_info_sorting_get_count(ets->sort_info)) + column = e_table_sort_info_sorting_get_nth(ets->sort_info, j); + else + column = e_table_sort_info_grouping_get_nth(ets->sort_info, j); + + if (column.column > e_table_header_count (ets->full_header)) + col = e_table_header_get_column (ets->full_header, e_table_header_count (ets->full_header) - 1); + else + col = e_table_header_get_column (ets->full_header, column.column); + + for (i = 0; i < rows; i++) { + vals_closure[i * cols + j] = e_table_model_value_at (ets->source, col->col_idx, i); + } + + compare_closure[j] = col->compare; + ascending_closure[j] = column.ascending; + } + qsort(ets->sorted, rows, sizeof(int), qsort_callback); + + g_free(vals_closure); + g_free(ascending_closure); + g_free(compare_closure); +} + +static void +ets_backsort(ETableSorter *ets) +{ + int i, rows; + + if (ets->backsorted) + return; + + ets_sort(ets); + + rows = e_table_model_row_count(ets->source); + ets->backsorted = g_new0(int, rows); + + for (i = 0; i < rows; i++) { + ets->backsorted[ets->sorted[i]] = i; + } +} + +gboolean +e_table_sorter_needs_sorting(ETableSorter *ets) +{ + if (ets->needs_sorting < 0) { + if (e_table_sort_info_sorting_get_count(ets->sort_info) + e_table_sort_info_grouping_get_count(ets->sort_info)) + ets->needs_sorting = 1; + else + ets->needs_sorting = 0; + } + return ets->needs_sorting; +} + + +gint +e_table_sorter_model_to_sorted (ETableSorter *sorter, int row) +{ + int rows = e_table_model_row_count(sorter->source); + + g_return_val_if_fail(row >= 0, -1); + g_return_val_if_fail(row < rows, -1); + + if (e_table_sorter_needs_sorting(sorter)) { + ets_backsort(sorter); + return sorter->backsorted[row]; + } else + return row; +} + +gint +e_table_sorter_sorted_to_model (ETableSorter *sorter, int row) +{ + int rows = e_table_model_row_count(sorter->source); + + g_return_val_if_fail(row >= 0, -1); + g_return_val_if_fail(row < rows, -1); + + if (e_table_sorter_needs_sorting(sorter)) { + ets_sort(sorter); + return sorter->sorted[row]; + } else + return row; +} diff --git a/widgets/table/e-table-sorter.h b/widgets/table/e-table-sorter.h new file mode 100644 index 0000000000..f5a71565a3 --- /dev/null +++ b/widgets/table/e-table-sorter.h @@ -0,0 +1,47 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +#ifndef _E_TABLE_SORTER_H_ +#define _E_TABLE_SORTER_H_ + +#include +#include "e-table-model.h" +#include "e-table-subset-variable.h" +#include "e-table-sort-info.h" +#include "e-table-header.h" + +#define E_TABLE_SORTER_TYPE (e_table_sorter_get_type ()) +#define E_TABLE_SORTER(o) (GTK_CHECK_CAST ((o), E_TABLE_SORTER_TYPE, ETableSorter)) +#define E_TABLE_SORTER_CLASS(k) (GTK_CHECK_CLASS_CAST((k), E_TABLE_SORTER_TYPE, ETableSorterClass)) +#define E_IS_TABLE_SORTER(o) (GTK_CHECK_TYPE ((o), E_TABLE_SORTER_TYPE)) +#define E_IS_TABLE_SORTER_CLASS(k) (GTK_CHECK_CLASS_TYPE ((k), E_TABLE_SORTER_TYPE)) + +typedef struct { + GtkObject base; + + ETableModel *source; + ETableHeader *full_header; + ETableSortInfo *sort_info; + + int needs_sorting; + + int *sorted; + int *backsorted; + + int table_model_changed_id; + int table_model_row_changed_id; + int table_model_cell_changed_id; + int sort_info_changed_id; +} ETableSorter; + +typedef struct { + GtkObjectClass parent_class; +} ETableSorterClass; + +GtkType e_table_sorter_get_type (void); +ETableSorter *e_table_sorter_new (ETableModel *etm, ETableHeader *full_header, ETableSortInfo *sort_info); + +gint e_table_sorter_model_to_sorted (ETableSorter *sorter, int row); +gint e_table_sorter_sorted_to_model (ETableSorter *sorter, int row); + +gboolean e_table_sorter_needs_sorting (ETableSorter *sorter); + +#endif /* _E_TABLE_SORTER_H_ */ diff --git a/widgets/table/e-table.c b/widgets/table/e-table.c index 4daddc3985..c6fffffd3d 100644 --- a/widgets/table/e-table.c +++ b/widgets/table/e-table.c @@ -144,6 +144,7 @@ et_destroy (GtkObject *object) gtk_object_unref (GTK_OBJECT (et->full_header)); gtk_object_unref (GTK_OBJECT (et->header)); gtk_object_unref (GTK_OBJECT (et->sort_info)); + gtk_object_unref (GTK_OBJECT (et->sorter)); gtk_widget_destroy (GTK_WIDGET (et->header_canvas)); gtk_widget_destroy (GTK_WIDGET (et->table_canvas)); @@ -187,6 +188,7 @@ e_table_init (GtkObject *object) e_table->drag_source_button_press_event_id = 0; e_table->drag_source_motion_notify_event_id = 0; + e_table->sorter = NULL; e_table->selection = e_table_selection_model_new(); e_table->cursor_loc = E_TABLE_CURSOR_LOC_NONE; } @@ -603,9 +605,6 @@ et_real_construct (ETable *e_table, ETableHeader *full_header, ETableModel *etm, e_table->model = etm; gtk_object_ref (GTK_OBJECT (etm)); - gtk_object_set (GTK_OBJECT (e_table->selection), - "model", etm, - NULL); gtk_widget_push_visual (gdk_rgb_get_visual ()); gtk_widget_push_colormap (gdk_rgb_get_cmap ()); @@ -617,6 +616,13 @@ et_real_construct (ETable *e_table, ETableHeader *full_header, ETableModel *etm, "sort_info", e_table->sort_info, NULL); + e_table->sorter = e_table_sorter_new(etm, e_table->full_header, e_table->sort_info); + + gtk_object_set (GTK_OBJECT (e_table->selection), + "model", etm, + "sorter", e_table->sorter, + NULL); + if (!no_header) { e_table_setup_header (e_table); } diff --git a/widgets/table/e-table.h b/widgets/table/e-table.h index 6f4fe153c6..a418a284a6 100644 --- a/widgets/table/e-table.h +++ b/widgets/table/e-table.h @@ -40,6 +40,7 @@ typedef struct { ETableGroup *group; ETableSortInfo *sort_info; + ETableSorter *sorter; ETableSelectionModel *selection; ETableCursorLoc cursor_loc; -- cgit v1.2.3