Commit 9629707e authored by Morten Welinder's avatar Morten Welinder

Sheet: unify code for insert/delete columns/rows.

parent 07f1ddc2
2014-12-15 Morten Welinder <terra@gnome.org>
* src/sheet-style.c (sheet_style_insdel_colrow): Rename from
sheet_style_insert_colrow and handled deletion too.
* src/sheet.c (sheet_insdel_colrow): Merge from sheet_insert_rows,
sheet_insert_cols, sheet_delete_rows, sheet_delete_cols. Simplify
and clean up.
* src/dependent.c (dependent_has_pos, dependent_move): New
functions.
2014-12-14 Morten Welinder <terra@gnome.org>
* src/sheet.c (sheet_colrow_add): Make sure the slot we use is
......
......@@ -239,7 +239,7 @@ dummy_dep_eval (G_GNUC_UNUSED GnmDependent *dep)
static void cell_dep_eval (GnmDependent *dep);
static void cell_dep_set_expr (GnmDependent *dep, GnmExprTop const *new_texpr);
static GSList *cell_dep_changed (GnmDependent *dep);
static GnmCellPos const *cell_dep_pos (GnmDependent const *dep);
static GnmCellPos *cell_dep_pos (GnmDependent const *dep);
static void cell_dep_debug_name (GnmDependent const *dep, GString *target);
static const GnmDependentClass cell_dep_class = {
cell_dep_eval,
......@@ -285,7 +285,7 @@ static const GnmDependentClass managed_dep_class = {
static void style_dep_eval (GnmDependent *dep);
static GSList *style_dep_changed (GnmDependent *dep);
static GnmCellPos const *style_dep_pos (GnmDependent const *dep);
static GnmCellPos *style_dep_pos (GnmDependent const *dep);
static void style_dep_debug_name (GnmDependent const *dep, GString *target);
static const GnmDependentClass style_dep_class = {
style_dep_eval,
......@@ -426,6 +426,14 @@ dependent_set_expr (GnmDependent *dep, GnmExprTop const *new_texpr)
}
}
gboolean
dependent_has_pos (GnmDependent const *dep)
{
int const t = dependent_type (dep);
GnmDependentClass *klass = g_ptr_array_index (dep_classes, t);
return klass->pos != NULL;
}
GnmCellPos const *
dependent_pos (GnmDependent const *dep)
{
......@@ -436,6 +444,20 @@ dependent_pos (GnmDependent const *dep)
return klass->pos ? klass->pos (dep) : &dummy;
}
void
dependent_move (GnmDependent *dep, int dx, int dy)
{
int const t = dependent_type (dep);
GnmDependentClass *klass = g_ptr_array_index (dep_classes, t);
GnmCellPos *pos;
g_return_if_fail (klass->pos != NULL);
pos = klass->pos (dep);
/* Need a virtual for this? */
pos->col += dx;
pos->row += dy;
}
/**
* dependent_set_sheet:
......@@ -1264,7 +1286,7 @@ cell_dep_changed (GnmDependent *dep)
return work;
}
static GnmCellPos const *
static GnmCellPos *
cell_dep_pos (GnmDependent const *dep)
{
return &GNM_DEP_TO_CELL (dep)->pos;
......@@ -1392,7 +1414,7 @@ style_dep_changed (GnmDependent *dep)
return NULL;
}
static GnmCellPos const *
static GnmCellPos *
style_dep_pos (GnmDependent const *dep)
{
return &((GnmStyleDependent*)dep)->pos;
......
......@@ -20,7 +20,7 @@ typedef struct {
void (*eval) (GnmDependent *dep);
void (*set_expr) (GnmDependent *dep, GnmExprTop const *new_texpr);
GSList* (*changed) (GnmDependent *dep);
GnmCellPos const* (*pos) (GnmDependent const *dep);
GnmCellPos* (*pos) (GnmDependent const *dep);
void (*debug_name) (GnmDependent const *dep, GString *target);
} GnmDependentClass;
......@@ -98,7 +98,9 @@ void dependent_add_dynamic_dep (GnmDependent *dep, GnmRangeRef const *rr);
gboolean dependent_is_volatile (GnmDependent *dep);
gboolean dependent_has_pos (GnmDependent const *dep);
GnmCellPos const *dependent_pos (GnmDependent const *dep);
void dependent_move (GnmDependent *dep, int dx, int dy);
GOUndo *dependents_relocate (GnmExprRelocateInfo const *info);
void dependents_link (GSList *deps);
......
......@@ -2124,56 +2124,63 @@ sheet_style_relocate (GnmExprRelocateInfo const *rinfo)
}
/**
* sheet_style_insert_colrow:
* sheet_style_insdel_colrow:
* @rinfo:
*
* A utility routine to give the effect of stretching the styles when a col/row
* is inserted. This is done by applying the styles from the left/top col/row
* to the new region.
* Insert of delete style columns/rows.
*
* For the insert case, we stretch the preceding column/row into there space
* we open.
*/
void
sheet_style_insert_colrow (GnmExprRelocateInfo const *rinfo)
sheet_style_insdel_colrow (GnmExprRelocateInfo const *rinfo)
{
GnmStyleList *styles = NULL;
Sheet *sheet;
GnmCellPos corner;
GnmStyleList *ptr, *styles = NULL;
GnmRange r;
gboolean is_insert;
g_return_if_fail (rinfo != NULL);
g_return_if_fail (rinfo->origin_sheet == rinfo->target_sheet);
/* 1) copy col/row to the top/left of the region, and extend it */
corner = rinfo->origin.start;
if (rinfo->col_offset != 0) {
int const o = rinfo->col_offset - 1;
int col = corner.col - 1;
if (col < 0)
col = 0;
corner.row = 0;
styles = sheet_style_get_range (rinfo->origin_sheet,
range_init (&r, col, 0, col, gnm_sheet_get_last_row (rinfo->origin_sheet)));
if (o > 0)
for (ptr = styles ; ptr != NULL ; ptr = ptr->next)
((GnmStyleRegion *)ptr->data)->range.end.col = o;
} else if (rinfo->row_offset != 0) {
int const o = rinfo->row_offset - 1;
int row = corner.row - 1;
if (row < 0)
row = 0;
corner.col = 0;
range_init_rows (&r, rinfo->origin_sheet, row, row);
styles = sheet_style_get_range (rinfo->origin_sheet, &r);
if (o > 0)
for (ptr = styles ; ptr != NULL ; ptr = ptr->next)
((GnmStyleRegion *)ptr->data)->range.end.row = o;
g_return_if_fail ((rinfo->col_offset == 0) != (rinfo->row_offset == 0));
is_insert = (rinfo->col_offset + rinfo->row_offset > 0);
sheet = rinfo->origin_sheet;
if (is_insert) {
/* 1) copy col/row to the top/left of the region, and extend it */
corner = rinfo->origin.start;
if (rinfo->col_offset) {
int col = MAX (corner.col - 1, 0);
GnmStyleList *ptr;
GnmRange r;
corner.row = 0;
range_init_cols (&r, sheet, col, col);
styles = sheet_style_get_range (sheet, &r);
for (ptr = styles ; ptr != NULL ; ptr = ptr->next) {
GnmStyleRegion *sr = ptr->data;
sr->range.end.col = rinfo->col_offset - 1;
}
} else {
int row = MAX (corner.row - 1, 0);
GnmStyleList *ptr;
GnmRange r;
corner.col = 0;
range_init_rows (&r, sheet, row, row);
styles = sheet_style_get_range (sheet, &r);
for (ptr = styles ; ptr != NULL ; ptr = ptr->next) {
GnmStyleRegion *sr = ptr->data;
sr->range.end.row = rinfo->row_offset - 1;
}
}
}
sheet_style_relocate (rinfo);
if (styles != NULL) {
sheet_style_set_list (rinfo->target_sheet, &corner, styles,
NULL, NULL);
if (styles) {
sheet_style_set_list (sheet, &corner, styles, NULL, NULL);
style_list_free (styles);
}
}
......
......@@ -41,7 +41,7 @@ void sheet_style_set_pos (Sheet *sheet, int col, int row,
void sheet_style_apply_pos (Sheet *sheet, int col, int row,
GnmStyle *style);
void sheet_style_insert_colrow (GnmExprRelocateInfo const *rinfo);
void sheet_style_insdel_colrow (GnmExprRelocateInfo const *rinfo);
void sheet_style_relocate (GnmExprRelocateInfo const *rinfo);
unsigned int sheet_style_find_conflicts (Sheet const *sheet, GnmRange const *r,
GnmStyle **style, GnmBorder **borders);
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment