From a077adb63e4789ff709d589825ef34061b0398e8 Mon Sep 17 00:00:00 2001 From: Tobias Predel Date: Fri, 8 Sep 2023 19:32:37 +0200 Subject: [PATCH 1/4] Emit cellcolor and/or textcolor changed in child When `cellcolor` or `textcolor` in a cell differs from the one of the parent cell, emit its value. See https://github.com/aardappel/treesheets/issues/502 for more details. --- src/cell.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cell.h b/src/cell.h index 976c94e0..b60d117e 100755 --- a/src/cell.h +++ b/src/cell.h @@ -237,12 +237,12 @@ struct Cell { str.Prepend(wxString() << text.stylebits); str.Prepend(L" stylebits=\""); } - if (cellcolor != doc->Background()) { + if (cellcolor != (parent ? parent->cellcolor : doc->Background())) { str.Prepend(L"\""); str.Prepend(wxString() << cellcolor); str.Prepend(L" colorbg=\""); } - if (textcolor != 0x000000) { + if (textcolor != (parent ? parent->textcolor : 0x000000)) { str.Prepend(L"\""); str.Prepend(wxString() << textcolor); str.Prepend(L" colorfg=\""); @@ -260,9 +260,9 @@ struct Cell { if (text.stylebits & STYLE_BOLD) style += L"font-weight: bold;"; if (text.stylebits & STYLE_ITALIC) style += L"font-style: italic;"; if (text.stylebits & STYLE_FIXED) style += L"font-family: monospace;"; - if (cellcolor != doc->Background()) + if (cellcolor != (parent ? parent->cellcolor : doc->Background())) style += wxString::Format(L"background-color: #%06X;", SwapColor(cellcolor)); - if (textcolor != 0x000000) + if (textcolor != (parent ? parent->textcolor : 0x000000)) style += wxString::Format(L"color: #%06X;", SwapColor(textcolor)); str.Prepend(style.IsEmpty() ? L"" : L""); str.Append(L' ', indent); From ff701c5437f2dda91863308695f216ee406833ad Mon Sep 17 00:00:00 2001 From: Tobias Predel Date: Sat, 9 Sep 2023 00:12:06 +0200 Subject: [PATCH 2/4] Emit text style changed in child Text styles like `font-weight`, `font-style` and `font-family` are automatically inherited in CSS. This commit makes the HTML export code only emit these styles when - the text style of the cell changes in comparison to the parent cell or - there is no parent cell. To not disturb the inheritance, this commit also removes the default CSS text styles from the header section. --- src/cell.h | 9 ++++++--- src/document.h | 3 +-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cell.h b/src/cell.h index b60d117e..00e14b35 100755 --- a/src/cell.h +++ b/src/cell.h @@ -257,9 +257,12 @@ struct Cell { str.Append(L"\n"); } else if (format == A_EXPHTMLT) { wxString style; - if (text.stylebits & STYLE_BOLD) style += L"font-weight: bold;"; - if (text.stylebits & STYLE_ITALIC) style += L"font-style: italic;"; - if (text.stylebits & STYLE_FIXED) style += L"font-family: monospace;"; + if (parent ? (text.stylebits & STYLE_BOLD) != (parent->text.stylebits & STYLE_BOLD) : true) + style += (text.stylebits & STYLE_BOLD) ? L"font-weight: bold;" : L"font-weight: normal;"; + if (parent ? (text.stylebits & STYLE_ITALIC) != (parent->text.stylebits & STYLE_ITALIC) : true) + style += (text.stylebits & STYLE_ITALIC) ? L"font-style: italic;" : L"font-style: normal;"; + if (parent ? (text.stylebits & STYLE_FIXED) != (parent->text.stylebits & STYLE_FIXED) : true) + style += (text.stylebits & STYLE_FIXED) ? L"font-family: monospace;" : L"font-family: sans-serif;"; if (cellcolor != (parent ? parent->cellcolor : doc->Background())) style += wxString::Format(L"background-color: #%06X;", SwapColor(cellcolor)); if (textcolor != (parent ? parent->textcolor : 0x000000)) diff --git a/src/document.h b/src/document.h index 0521c0c5..460cd186 100755 --- a/src/document.h +++ b/src/document.h @@ -879,8 +879,7 @@ struct Document { L"\n\n\n" L"export of TreeSheets file "); dos.WriteString(filename); From 1abd139d1682abf50e6353f73229ec6c3bef157b Mon Sep 17 00:00:00 2001 From: Tobias Predel <tobias.predel@gmail.com> Date: Sat, 9 Sep 2023 01:32:43 +0200 Subject: [PATCH 3/4] Use flag for style inheritance Style inheritance should be used for the HTML export of the entire document but not for the HTML export of the selection to the clipboard. This commit adds the capability for both ways. --- src/cell.h | 16 ++++++++-------- src/document.h | 8 ++++---- src/grid.h | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/cell.h b/src/cell.h index 00e14b35..44d85a1d 100755 --- a/src/cell.h +++ b/src/cell.h @@ -207,7 +207,7 @@ struct Cell { bool IsParentOf(const Cell *c) { return c->parent == this || (c->parent && IsParentOf(c->parent)); } uint SwapColor(uint c) { return ((c & 0xFF) << 16) | (c & 0xFF00) | ((c & 0xFF0000) >> 16); } - wxString ToText(int indent, const Selection &s, int format, Document *doc) { + wxString ToText(int indent, const Selection &s, int format, Document *doc, bool inheritstyle) { wxString str = text.ToText(indent, s, format); if (format == A_EXPHTMLT && ((text.stylebits & STYLE_UNDERLINE) || (text.stylebits & STYLE_STRIKETHRU))) { wxString spanstyle = L"text-decoration:"; @@ -218,13 +218,13 @@ struct Cell { str.Append(L"</span>"); } if (format == A_EXPCSV) { - if (grid) return grid->ToText(indent, s, format, doc); + if (grid) return grid->ToText(indent, s, format, doc, inheritstyle); str.Replace(L"\"", L"\"\""); return L"\"" + str + L"\""; } if (s.cursor != s.cursorend) return str; str.Append(L"\n"); - if (grid) str.Append(grid->ToText(indent, s, format, doc)); + if (grid) str.Append(grid->ToText(indent, s, format, doc, inheritstyle)); if (format == A_EXPXML) { str.Prepend(L">"); if (text.relsize) { @@ -257,15 +257,15 @@ struct Cell { str.Append(L"</cell>\n"); } else if (format == A_EXPHTMLT) { wxString style; - if (parent ? (text.stylebits & STYLE_BOLD) != (parent->text.stylebits & STYLE_BOLD) : true) + if (!inheritstyle || !parent || (text.stylebits & STYLE_BOLD) != (parent->text.stylebits & STYLE_BOLD)) style += (text.stylebits & STYLE_BOLD) ? L"font-weight: bold;" : L"font-weight: normal;"; - if (parent ? (text.stylebits & STYLE_ITALIC) != (parent->text.stylebits & STYLE_ITALIC) : true) + if (!inheritstyle || !parent || (text.stylebits & STYLE_ITALIC) != (parent->text.stylebits & STYLE_ITALIC)) style += (text.stylebits & STYLE_ITALIC) ? L"font-style: italic;" : L"font-style: normal;"; - if (parent ? (text.stylebits & STYLE_FIXED) != (parent->text.stylebits & STYLE_FIXED) : true) + if (!inheritstyle || !parent || (text.stylebits & STYLE_FIXED) != (parent->text.stylebits & STYLE_FIXED)) style += (text.stylebits & STYLE_FIXED) ? L"font-family: monospace;" : L"font-family: sans-serif;"; - if (cellcolor != (parent ? parent->cellcolor : doc->Background())) + if (!inheritstyle || cellcolor != (parent ? parent->cellcolor : doc->Background())) style += wxString::Format(L"background-color: #%06X;", SwapColor(cellcolor)); - if (textcolor != (parent ? parent->textcolor : 0x000000)) + if (!inheritstyle || textcolor != (parent ? parent->textcolor : 0x000000)) style += wxString::Format(L"color: #%06X;", SwapColor(textcolor)); str.Prepend(style.IsEmpty() ? L"<td>" : L"<td style=\"" + style + L"\">"); str.Append(L' ', indent); diff --git a/src/document.h b/src/document.h index 460cd186..1d5aa541 100755 --- a/src/document.h +++ b/src/document.h @@ -412,7 +412,7 @@ struct Document { auto CopyEntireCells(wxString &s) { sys->clipboardcopy = s; - wxString html = selected.g->ConvertToText(selected, 0, A_EXPHTMLT, this); + wxString html = selected.g->ConvertToText(selected, 0, A_EXPHTMLT, this, false); auto htmlobj = #ifdef __WXGTK__ new wxCustomDataObject(wxDF_HTML); @@ -438,7 +438,7 @@ struct Document { dragdata.Add(new wxBitmapDataObject(bm)); } } else { - wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this); + wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this, false); dragdata.Add(new wxTextDataObject(s)); if (!selected.TextEdit()) { auto htmlobj = CopyEntireCells(s); @@ -476,7 +476,7 @@ struct Document { } } else { wxDataObjectComposite *clipboarddata = new wxDataObjectComposite(); - wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this); + wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this, false); clipboarddata->Add(new wxTextDataObject(s)); if (!selected.TextEdit()) { auto htmlobj = CopyEntireCells(s); @@ -859,7 +859,7 @@ struct Document { return _(L"Error writing to file!"); } wxTextOutputStream dos(fos); - wxString content = root->ToText(0, Selection(), k, this); + wxString content = root->ToText(0, Selection(), k, this, true); switch (k) { case A_EXPXML: dos.WriteString( diff --git a/src/grid.h b/src/grid.h index 9c9bd5da..e5c5b516 100755 --- a/src/grid.h +++ b/src/grid.h @@ -598,11 +598,11 @@ struct Grid { } } - wxString ToText(int indent, const Selection &s, int format, Document *doc) { - return ConvertToText(SelectAll(), indent + 2, format, doc); + wxString ToText(int indent, const Selection &s, int format, Document *doc, bool inheritstyle) { + return ConvertToText(SelectAll(), indent + 2, format, doc, inheritstyle); }; - wxString ConvertToText(const Selection &s, int indent, int format, Document *doc) { + wxString ConvertToText(const Selection &s, int indent, int format, Document *doc, bool inheritstyle) { wxString r; const int root_grid_spacing = 2; // Can't be adjusted in editor, so use a default. const int font_size = 14 - indent / 2; @@ -616,7 +616,7 @@ struct Grid { font_size).wc_str()); foreachcellinsel(c, s) { if (x == 0) Formatter(r, format, indent, L"<row>\n", L"<tr>\n", L""); - r.Append(c->ToText(indent, s, format, doc)); + r.Append(c->ToText(indent, s, format, doc, inheritstyle)); if (format == A_EXPCSV) r.Append(x == xs - 1 ? '\n' : ','); if (x == xs - 1) Formatter(r, format, indent, L"</row>\n", L"</tr>\n", L""); } From 965eb224a06a051507253819396783b64bc222c6 Mon Sep 17 00:00:00 2001 From: Tobias Predel <tobias.predel@gmail.com> Date: Sat, 9 Sep 2023 01:53:14 +0200 Subject: [PATCH 4/4] Emit XML stylebits changed in child --- src/cell.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cell.h b/src/cell.h index 44d85a1d..79e52e7a 100755 --- a/src/cell.h +++ b/src/cell.h @@ -232,7 +232,7 @@ struct Cell { str.Prepend(wxString() << -text.relsize); str.Prepend(L" relsize=\""); } - if (text.stylebits) { + if (parent ? text.stylebits ^ parent->text.stylebits : text.stylebits) { str.Prepend(L"\""); str.Prepend(wxString() << text.stylebits); str.Prepend(L" stylebits=\"");