[gnumeric] Resize: improve style handling on sheet expansion.



commit 6b284be1984f7dff4b9365d25ce48476e9203b98
Author: Morten Welinder <terra gnome org>
Date:   Sun Feb 23 20:59:23 2014 -0500

    Resize: improve style handling on sheet expansion.
    
    When expanding a sheet, fill in the new areas with most common styles
    for the rows to the left of the new columns or the columns above
    the new area.

 ChangeLog         |    6 ++++
 NEWS              |    1 +
 src/sheet-style.c |    9 ++++++
 src/sheet.c       |   79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 95 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 4cda3c3..2676263 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2014-02-23  Morten Welinder  <terra gnome org>
+
+       * src/sheet.c (gnm_sheet_resize_main): When expanding a sheet,
+       fill in the new areas with most common styles for the rows to the
+       left of the new columns or the columns above the new area.
+
 2014-02-19  Morten Welinder  <terra gnome org>
 
        * plugins/fn-math/functions.c (gnumeric_log2): Use gnm_log2.
diff --git a/NEWS b/NEWS
index 4341e84..484ccd0 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,7 @@ Morten:
        * Fix xls import of cells with long string results.
        * Fix xls export of long strings in various places.
        * Fix BIFF7 export of comments.
+       * Improve style handling when expanding sheets. 
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.11
diff --git a/src/sheet-style.c b/src/sheet-style.c
index cf2cf63..a61c06f 100644
--- a/src/sheet-style.c
+++ b/src/sheet-style.c
@@ -2367,6 +2367,15 @@ cb_most_common (GnmStyle *style,
                        counts[corner_row + i] += width;
 }
 
+/**
+ * sheet_style_most_common:
+ *
+ * @sheet: sheet to inspect
+ * @is_col: if %TRUE, look for common styles in columns; if FALSE, look in rows.
+ *
+ * Returns: an array of styles describing the most common styles, one per column
+ * or row.
+ */
 GnmStyle **
 sheet_style_most_common (Sheet const *sheet, gboolean is_col)
 {
diff --git a/src/sheet.c b/src/sheet.c
index 01682f3..51249c7 100644
--- a/src/sheet.c
+++ b/src/sheet.c
@@ -1200,6 +1200,8 @@ gnm_sheet_resize_main (Sheet *sheet, int cols, int rows,
                       GOCmdContext *cc, GOUndo **pundo)
 {
        int old_cols, old_rows;
+       GnmStyle **common_col_styles = NULL;
+       GnmStyle **common_row_styles = NULL;
 
        if (pundo) *pundo = NULL;
 
@@ -1209,6 +1211,22 @@ gnm_sheet_resize_main (Sheet *sheet, int cols, int rows,
                return;
 
        /* ---------------------------------------- */
+       /* Gather styles we want to copy into new areas.  */
+
+       if (cols > old_cols) {
+               int r;
+               common_row_styles = sheet_style_most_common (sheet, FALSE);
+               for (r = 0; r < old_rows; r++)
+                       gnm_style_ref (common_row_styles[r]);
+       }
+       if (rows > old_rows) {
+               int c;
+               common_col_styles = sheet_style_most_common (sheet, TRUE);
+               for (c = 0; c < old_cols; c++)
+                       gnm_style_ref (common_col_styles[c]);
+       }
+
+       /* ---------------------------------------- */
        /* Remove the columns and rows that will disappear.  */
 
        if (cols < old_cols) {
@@ -1330,6 +1348,67 @@ gnm_sheet_resize_main (Sheet *sheet, int cols, int rows,
        range_init_full_sheet (&sheet->priv->unhidden_region, sheet);
 
        /* ---------------------------------------- */
+       /* Apply styles to new areas.  */
+
+       if (cols > old_cols) {
+               int r = 0;
+               while (r < old_rows) {
+                       int r2 = r;
+                       GnmStyle *mstyle = common_row_styles[r];
+                       GnmRange rng;
+                       while (r2 + 1 < old_rows &&
+                              mstyle == common_row_styles[r2 + 1])
+                               r2++;
+                       range_init (&rng, old_cols, r, cols - 1, r2);
+                       gnm_style_ref (mstyle);
+                       sheet_apply_style (sheet, &rng, mstyle);
+                       r = r2 + 1;
+               }
+
+               for (r = 0; r < old_rows; r++)
+                       gnm_style_unref (common_row_styles[r]);
+
+               g_free (common_row_styles);
+       }
+
+       if (rows > old_rows) {
+               int c = 0;
+
+               while (c < old_cols) {
+                       int c2 = c;
+                       GnmStyle *mstyle = common_col_styles[c];
+                       GnmRange rng;
+                       while (c2 + 1 < old_cols &&
+                              mstyle == common_col_styles[c2 + 1])
+                               c2++;
+                       range_init (&rng, c, old_rows, c2, rows - 1);
+                       gnm_style_ref (mstyle);
+                       sheet_apply_style (sheet, &rng, mstyle);
+                       c = c2 + 1;
+               }
+
+               if (cols > old_cols) {
+                       /*
+                        * Expanded in both directions.  One could argue about
+                        * what style to use down here, but we choose the
+                        * last column style.
+                        */
+                       GnmStyle *mstyle = common_col_styles[old_cols - 1];
+                       GnmRange rng;
+
+                       range_init (&rng,
+                                   old_cols, old_rows,
+                                   cols - 1, rows - 1);
+                       gnm_style_ref (mstyle);
+                       sheet_apply_style (sheet, &rng, mstyle);
+               }
+
+               for (c = 0; c < old_cols; c++)
+                       gnm_style_unref (common_col_styles[c]);
+               g_free (common_col_styles);
+       }
+
+       /* ---------------------------------------- */
 
        sheet_redraw_all (sheet, TRUE);
        return;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]