/* The evaluation types for a cell. CT_DATA: "Data" CT_CODE: "Operation" CT_VARD: "Variable Assign" CT_VARU: "Variable Read" CT_VIEWH: "Horizontal View" CT_VIEWV: "Vertical View" CT_FORMULA: "Formula", e.g. =SUM(Expenses), whose text is its result (see FormulaEvaluator) */ enum { CT_DATA = 0, CT_CODE, CT_VARD, CT_VIEWH, CT_VARU, CT_VIEWV, CT_FORMULA }; /* The drawstyles for a cell: */ enum { DS_GRID, DS_BLOBSHIER, DS_BLOBLINE }; /** The Cell structure represents the editable cells in the sheet. They are mutable structures containing a text and grid object. Along with formatting information. */ struct Cell { Cell *parent; int sx {0}; int sy {0}; int ox {0}; int oy {0}; int minx {0}; int miny {0}; int ycenteroff {0}; int ycenterbase {0}; // ycenteroff before the vertical alignment, see AlignContent() int txs {0}; int tys {0}; // The room right of a grid beside the text, which the text and the grid are aligned in // together, or -1 if the grid isn't beside the text. See AlignContent(). int roombesidegrid {-1}; int celltype; Text text; shared_ptr grid; uint cellcolor {g_cellcolor_default}; uint actualcellcolor {g_cellcolor_default}; uint textcolor {g_textcolor_default}; bool tiny {false}; bool verticaltextandgrid {true}; wxUint8 drawstyle {DS_GRID}; wxUint8 textalign {TEXTALIGN_AUTO}; wxUint8 vertalign {VERTALIGN_AUTO}; // Whether a partial undo snapshot that is still pending reuses this cell, see // Document::FinishUndo(). bool reusedbyundo {false}; wxString note; shared_ptr formula; // for CT_FORMULA, see SyncFormula() Cell(Cell *_p = nullptr, const Cell *_clonefrom = nullptr, int _ct = CT_DATA, const shared_ptr &_g = nullptr) : parent(_p), celltype(_ct), grid(_g) { text.cell = this; if (_g != nullptr) { _g->cell = this; } if (_p != nullptr) { text.relsize = _p->text.relsize; verticaltextandgrid = _p->verticaltextandgrid; } if (_clonefrom != nullptr) { CloneStyleFrom(_clonefrom); } } ~Cell() { if (reusedbyundo) { Document::RescueForUndo(this); } } void Clear() { if (celltype == CT_FORMULA) { celltype = CT_DATA; } formula = nullptr; grid = nullptr; text.SetText(wxEmptyString); text.image = nullptr; Reset(); } bool HasText() const { return !text.t.empty(); } bool HasTextSize() const { return HasText() || text.relsize != 0; } bool HasTextState() const { return HasTextSize() || text.image != nullptr; } bool HasHeader() const { return HasText() || text.image != nullptr; } bool HasContent() const { return HasHeader() || grid; } bool TinyOrFiltered() const { return tiny || text.filtered; } bool GridShown(Document *doc) const { return grid && (!grid->folded || this == doc->currentdrawroot); } int MinRelsize() // the smallest relsize is actually the biggest text { int rs = INT_MAX; if (grid) { rs = grid->MinRelsize(rs); } else if (HasText()) { // the "else" causes oversized titles but a readable grid when you zoom, if only // the grid has been shrunk rs = text.MinRelsize(rs); } return rs; } size_t EstimatedMemoryUse() { return sizeof(Cell) + text.EstimatedMemoryUse() + (grid ? grid->EstimatedMemoryUse() : 0); } template void Layout(Document *doc, DC &dc, int depth, int maxcolwidth, bool forcetiny) { bool wastiny = tiny; tiny = text.filtered && !grid || forcetiny || doc->PickFont(dc, depth, text.relsize, text.stylebits); // The cells inside are tiny along with this one. if (grid && tiny != wastiny) { for (auto &c : grid->cells) { c->ResetChildren(); } } int ixs = 0; int iys = 0; if (!tiny) { treesheets::System::ImageSize(text.DisplayImage(), ixs, iys); } int leftoffset = 0; if (!HasText()) { if (ixs == 0 || iys == 0) { sx = sy = tiny ? 1 : doc->CharHeight(dc); } else { leftoffset = doc->CharHeight(dc); } } else { text.TextSize(dc, sx, sy, static_cast(tiny), leftoffset, maxcolwidth, doc, depth); } if (ixs != 0 && iys != 0) { sx += ixs + 2; sy = max(iys + 2, sy); } text.extent = sx + depth * doc->CharHeight(dc); txs = sx; tys = sy; if (GridShown(doc)) { if (HasHeader()) { if (verticaltextandgrid) { int osx = sx; if (drawstyle == DS_BLOBLINE && !tiny) { sy += 4; } grid->Layout(doc, dc, depth, sx, sy, leftoffset, sy, tiny || forcetiny); sx = max(sx, osx); } else { int osy = sy; if (drawstyle == DS_BLOBLINE && !tiny) { sx += 18; } grid->Layout(doc, dc, depth, sx, sy, sx, 0, tiny || forcetiny); sy = max(sy, osy); } } else { tiny = grid->Layout(doc, dc, depth, sx, sy, 0, 0, forcetiny); } } ycenteroff = ycenterbase = !verticaltextandgrid ? (sy - tys) / 2 : 0; if (!tiny) { sx += g_margin_extra * 2; sy += g_margin_extra * 2; } } template void Render(Document *doc, int bx, int by, DCType &dc, int depth, int ml, int mr, int mt, int mb, int maxcolwidth, int cell_margin) { // Choose color from celltype (program operations) switch (celltype) { case CT_VARD: actualcellcolor = sys->varassigncolor; break; case CT_VARU: actualcellcolor = sys->varreadcolor; break; case CT_VIEWH: case CT_VIEWV: actualcellcolor = sys->viewcolor; break; case CT_CODE: actualcellcolor = sys->operationcolor; break; default: actualcellcolor = cellcolor; break; } if (IsTag(doc)) actualcellcolor = doc->tags[text.t].first; uint parentcolor = doc->Background(); if (parent != nullptr && this != doc->currentdrawroot) { Cell *p = parent; while (p != nullptr && p->drawstyle == DS_BLOBLINE) { p = p == doc->currentdrawroot ? nullptr : p->parent; } if (p != nullptr) { parentcolor = p->actualcellcolor; } } if (sys->darkennonmatchingcells && !text.IsInSearch()) { auto *cp = reinterpret_cast(&actualcellcolor); loop(i, 4) cp[i] = cp[i] * 800 / 1000; } if (drawstyle == DS_GRID && actualcellcolor != parentcolor) { DrawRectangle(dc, actualcellcolor, bx - ml, by - mt, sx + ml + mr, sy + mt + mb); } const wxColour wxactualcellcolor = LightColor(actualcellcolor); if (drawstyle != DS_GRID && HasContent() && !tiny) { if (actualcellcolor == parentcolor) { auto *cp = reinterpret_cast(&actualcellcolor); loop(i, 4) cp[i] = cp[i] * 850 / 1000; } if (drawstyle == DS_BLOBSHIER) { DrawRoundedRectangle(dc, wxactualcellcolor, bx - cell_margin, by - cell_margin, minx + cell_margin * 2, miny + cell_margin * 2, sys->roundness); } else if (HasHeader()) { DrawRoundedRectangle(dc, wxactualcellcolor, bx - cell_margin + g_margin_extra / 2, by - cell_margin + ycenteroff + g_margin_extra / 2, txs + cell_margin * 2 + g_margin_extra, tys + cell_margin * 2 + g_margin_extra, sys->roundness); // FIXME: this half a g_margin_extra is a bit of hack } } dc.SetTextBackground(wxactualcellcolor); int xoff = verticaltextandgrid ? 0 : text.extent - depth * doc->CharHeight(dc); int yoff = text.Render(doc, bx, by + ycenteroff, depth, dc, xoff, maxcolwidth); yoff = verticaltextandgrid ? yoff : 0; if (GridShown(doc)) { grid->Render(doc, bx, by, dc, depth, sx - xoff, sy - yoff, xoff, yoff); } if (ShowsNote(doc)) { DrawNote(dc, NoteRect(bx + sx + mr, by - mt), LightColor(textcolor)); } } bool ShowsNote(Document *doc) const { return !note.IsEmpty() && !tiny && this != doc->currentdrawroot; } // The triangle that marks a note, in the top-right corner at right, top (outside the margins // of the cell). static wxRect NoteRect(int right, int top) { return {right - 6, top, 7, 7}; } template static void DrawNote(DC &dc, const wxRect &r, const wxColour &color) { wxPoint points[] = {{r.GetRight(), r.y}, {r.GetRight(), r.GetBottom()}, {r.x, r.y}}; dc.SetBrush(wxBrush(color)); dc.SetPen(wxPen(color)); dc.DrawPolygon(3, points); } void CloneStyleFrom(Cell const *o) { cellcolor = o->cellcolor; textcolor = o->textcolor; verticaltextandgrid = o->verticaltextandgrid; drawstyle = o->drawstyle; textalign = o->textalign; vertalign = o->vertalign; text.stylebits = o->text.stylebits; } // The alignment the text is drawn with: never TEXTALIGN_AUTO. int TextAlign() const { if (textalign != TEXTALIGN_AUTO) { return textalign; } return text.IsRightToLeft() ? TEXTALIGN_RIGHT : TEXTALIGN_LEFT; } // The width the lines of text are aligned in, from the left edge of the text (after the // image). Text lines up with the whole cell, except beside its grid, where it moves along // with the grid by the room right of it (see AlignContent()). Text drawn as a blob only has // its own width, so there alignment only lines up the lines with each other. int TextAlignWidth(int ixs) const { auto w = drawstyle != DS_GRID ? txs : roombesidegrid >= 0 ? txs + roombesidegrid : sx - g_margin_extra * 2; return w - ixs - 4; } // Moves the content of the cell by its alignment, once it got the size of its column and row. // // Horizontally that's the grid (not the alignment of the cells in it, which have their own), // within the room below or beside the text. Like the text, a grid drawn as a blob only has // the cell's own width, so that it stays inside the blob, while in a grid it lines up with // the whole cell, which is as wide as its column. // // Vertically, the text and the grid move together in the height of the row. A bubble has // the cell's own height, so there nothing moves. void AlignContent(Document *doc) { auto vroom = drawstyle == DS_BLOBSHIER ? 0 : max(sy - miny, 0); auto valign = vertalign == VERTALIGN_AUTO ? (drawstyle == DS_BLOBLINE && !grid ? VERTALIGN_MIDDLE : VERTALIGN_TOP) : vertalign; auto vshift = valign == VERTALIGN_BOTTOM ? vroom : valign == VERTALIGN_MIDDLE ? vroom / 2 : 0; ycenteroff = ycenterbase + vshift; roombesidegrid = -1; if (!GridShown(doc)) { return; } grid->ShiftY(vshift); auto width = (drawstyle == DS_GRID ? sx : minx) - (tiny ? 0 : g_margin_extra * 2); // The indent of a grid below the text only matters for left-aligned text, except for the // lines of line style, which run in it. auto from = verticaltextandgrid && drawstyle == DS_GRID ? 0 : grid->start; auto room = width - from - (grid->extent - grid->start); if (!verticaltextandgrid) { roombesidegrid = max(room, 0); } auto align = TextAlign(); auto left = align == TEXTALIGN_RIGHT ? from + room : align == TEXTALIGN_CENTER ? from + room / 2 : grid->start; grid->ShiftX(max(left, from) - grid->start); } // With sharegrid, the clone has the same grid as this cell rather than a copy of it. unique_ptr Clone(Cell *_parent, bool sharegrid = false) const { auto g = grid && !sharegrid ? make_shared(grid->xs, grid->ys) : nullptr; unique_ptr c = make_unique(_parent, this, celltype, g); c->text = text; c->text.cell = c.get(); c->note = note; c->formula = formula; if (grid) { if (sharegrid) { c->grid = grid; } else { grid->Clone(c->grid); } } return c; } bool IsInside(int x, int y) const { return x >= 0 && y >= 0 && x < sx && y < sy; } int GetX(Document *doc) const { return ox + (parent != nullptr ? parent->GetX(doc) : doc->hierarchysize); } int GetY(Document *doc) const { return oy + (parent != nullptr ? parent->GetY(doc) : doc->hierarchysize); } int Depth() const { return parent != nullptr ? parent->Depth() + 1 : 0; } Cell *Parent(int i) { return i != 0 ? parent->Parent(i - 1) : this; } Cell *SetParent(Cell *g) { parent = g; return this; } bool IsParentOf(const Cell *c) { return c->parent == this || (c->parent != nullptr && IsParentOf(c->parent)); } wxString ToText(int indent, const Selection &sel, int format, Document *doc, bool inheritstyle, Cell *root, bool flat = false) { wxString str = flat && TinyOrFiltered() ? wxString() : format == A_EXPXML && formula ? Text::htmlify(*formula) : text.ToText(indent, sel, format, doc); if (auto it = doc->htmlanchors.find(this); it != doc->htmlanchors.end() && Text::IsHTMLFormat(format) && !str.IsEmpty()) { str.Prepend(wxString::Format("", it->second)); } if ((format == A_EXPHTMLT || format == A_EXPHTMLTI || format == A_EXPHTMLTE) && this != root && !str.IsEmpty()) { wxString spanstyle = "white-space: pre-wrap;"; if (text.runs.empty() && (text.stylebits & (STYLE_UNDERLINE | STYLE_STRIKETHRU)) != 0) { spanstyle += "text-decoration:"; spanstyle += (text.stylebits & STYLE_UNDERLINE) != 0 ? " underline" : ""; spanstyle += (text.stylebits & STYLE_STRIKETHRU) != 0 ? " line-through" : ""; spanstyle += ";"; } str.Prepend(""); str.Append(""); } if (format == A_EXPCSV || format == A_EXPTSV) { if (grid && this == root) { return grid->ToText(indent, sel, format, doc, inheritstyle, root); } if (format == A_EXPTSV && !str.StartsWith("\"") && str.Find('\t') < 0) { return str; } str.Replace("\"", "\"\""); return "\"" + str + "\""; } if (sel.cursor != sel.cursorend) { return str; } str.Append(LINE_SEPARATOR); if (grid && !flat) { str.Append(grid->ToText(indent, sel, format, doc, inheritstyle, root)); } if (format == A_EXPXML) { str.Prepend(">"); if (text.relsize != 0) { str.Prepend("\""); str.Prepend(wxString() << -text.relsize); str.Prepend(" relsize=\""); } if (text.stylebits != 0) { str.Prepend("\""); str.Prepend(wxString() << text.stylebits); str.Prepend(" stylebits=\""); } if (cellcolor != 0xFFFFFF) { str.Prepend("\""); str.Prepend(wxString::Format("0x%06X", cellcolor)); str.Prepend(" colorbg=\""); } if (textcolor != 0x000000) { str.Prepend("\""); str.Prepend(wxString::Format("0x%06X", textcolor)); str.Prepend(" colorfg=\""); } if (celltype != CT_DATA) { str.Prepend("\""); str.Prepend(wxString() << celltype); str.Prepend(" type=\""); } if (textalign != TEXTALIGN_AUTO) { str.Prepend("\""); str.Prepend(wxString() << static_cast(textalign)); str.Prepend(" align=\""); } if (vertalign != VERTALIGN_AUTO) { str.Prepend("\""); str.Prepend(wxString() << static_cast(vertalign)); str.Prepend(" valign=\""); } str.Prepend("\n"); } else if ((format == A_EXPHTMLT || format == A_EXPHTMLTI || format == A_EXPHTMLTE) && this != root) { wxString style; if (!inheritstyle || parent == nullptr || (text.stylebits & STYLE_BOLD) != (parent->text.stylebits & STYLE_BOLD)) { style += (text.stylebits & STYLE_BOLD) != 0 ? "font-weight: bold;" : "font-weight: normal;"; } if (!inheritstyle || parent == nullptr || (text.stylebits & STYLE_ITALIC) != (parent->text.stylebits & STYLE_ITALIC)) { style += (text.stylebits & STYLE_ITALIC) != 0 ? "font-style: italic;" : "font-style: normal;"; } if (!inheritstyle || parent == nullptr || (text.stylebits & STYLE_FIXED) != (parent->text.stylebits & STYLE_FIXED)) { style += "font-family: '"; style += (text.stylebits & STYLE_FIXED) != 0 ? sys->defaultfixedfont + "', monospace;" : sys->defaultfont + "', sans-serif;"; } // Nested tables inherit text-align, so give it wherever it differs from the parent. auto align = TextAlign(); auto parentalign = parent != nullptr && parent != root ? parent->TextAlign() : TEXTALIGN_LEFT; if (align != parentalign) { static const char *const aligns[] = {"", "left", "center", "right"}; style += wxString("text-align: ") + aligns[align] + ";"; } // The page makes cells align at the top. if (vertalign == VERTALIGN_MIDDLE || vertalign == VERTALIGN_BOTTOM) { style += vertalign == VERTALIGN_MIDDLE ? "vertical-align: middle;" : "vertical-align: bottom;"; } auto exportcellcolor = IsTag(doc) ? doc->tags[text.t].first : cellcolor; auto parentcellcolor = parent != nullptr ? parent->IsTag(doc) ? doc->tags[parent->text.t].first : parent->cellcolor : doc->Background(); if (!inheritstyle || exportcellcolor != parentcellcolor) { style += wxString::Format("background-color: #%06X;", SwapColor(cellcolor)); } auto exporttextcolor = IsTag(doc) ? doc->tags[text.t].second : textcolor; auto parenttextcolor = parent != nullptr ? parent->IsTag(doc) ? doc->tags[parent->text.t].second : parent->textcolor : 0x000000; if (!inheritstyle || exporttextcolor != parenttextcolor) { style += wxString::Format("color: #%06X;", SwapColor(exporttextcolor)); } str.Prepend(style.IsEmpty() ? wxString("") : wxString("")); str.Append(' ', indent); str.Append("\n"); } else if (format == A_EXPHTMLB && (!text.t.IsEmpty() || grid) && this != root) { str.Prepend("
  • "); str.Append(' ', indent); str.Append("
  • \n"); } else if (format == A_EXPHTMLO && !text.t.IsEmpty()) { wxString h = wxString("h") + static_cast('0' + indent / 2) + ">"; str.Prepend("<" + h); str.Append(' ', indent); str.Append("RelSize(dir, zoomdepth); } } void Reset() { ox = oy = sx = sy = minx = miny = ycenteroff = 0; } void ResetChildren() { Reset(); if (grid) { grid->ResetChildren(); } } void ResetLayout() { Reset(); if (parent != nullptr) { parent->ResetLayout(); } } template void LazyLayout(Document *doc, DC &dc, int depth, int maxcolwidth, bool forcetiny) { if (sx == 0) { Layout(doc, dc, depth, maxcolwidth, forcetiny); minx = sx; miny = sy; } else { sx = minx; sy = miny; } } static bool IsFormulaText(const wxString &s) { return s.Len() > 1 && s[0] == '='; } // Makes the formula match the cell type: a cell that became a formula gets its text as // formula, until its result replaces it (see Document::RecalcFormulas()). void SyncFormula() { if (celltype != CT_FORMULA) { formula = nullptr; } else if (!formula) { formula = make_shared(text.t.StartsWith("=") ? text.t : "=" + text.t); sys->formulasseen = true; } } void AddUndo(Document *doc, bool textedit = false) { ResetLayout(); doc->AddUndo(this, true, textedit); } // For an action that changes only this cell, its grid and the cells of scope in it (none for // one that only moves or deletes cells), see Document::AddUndo(). void AddUndo(Document *doc, const Selection &scope, bool recursive) { ResetLayout(); doc->AddUndo(this, true, false, &scope, recursive); } void Save(wxDataOutputStream &dos, Cell *ocs) const { dos.Write8(celltype); dos.Write32(cellcolor); dos.Write32(textcolor); dos.Write8(drawstyle); dos.WriteString(note); dos.Write8(textalign); dos.Write8(vertalign); uint cellflags = this == ocs ? TS_SELECTION_MASK : 0; if (HasTextState() || formula) { cellflags |= grid ? TS_BOTH : TS_TEXT; dos.Write8(cellflags); text.Save(dos, formula.get()); if (grid) { grid->Save(dos, ocs); } } else if (grid) { cellflags |= TS_GRID; dos.Write8(cellflags); grid->Save(dos, ocs); } else { cellflags |= TS_NEITHER; dos.Write8(cellflags); } } Grid* AddGrid(int x = 1, int y = 1) { if (!grid) { grid = make_shared(x, y, this); grid->InitCells(this); if (parent != nullptr) { grid->CloneStyleFrom(parent->grid.get()); } } return grid.get(); } Cell *LoadGrid(wxDataInputStream &dis, int &numcells, int &textbytes, Cell *&ics) { int xs = dis.Read32(); int ys = dis.Read32(); if (xs < 1 || ys < 1 || static_cast(xs) * ys > g_max_grid_cells || Depth() >= g_max_depth) { return nullptr; } auto g = make_shared(xs, ys); grid = g; g->cell = this; if (!g->LoadContents(dis, numcells, textbytes, ics)) { return nullptr; } return this; } static Cell *LoadWhich(wxDataInputStream &dis, Cell *_p, int &numcells, int &textbytes, Cell *&ics) { auto c = make_unique(_p, nullptr, dis.Read8()); numcells++; if (sys->versionlastloaded >= 8) { c->cellcolor = dis.Read32() & 0xFFFFFF; c->textcolor = dis.Read32() & 0xFFFFFF; } if (sys->versionlastloaded >= 15) { c->drawstyle = dis.Read8(); } if (sys->versionlastloaded >= 25) { c->note = dis.ReadString(); } if (sys->versionlastloaded >= 28) { c->textalign = dis.Read8(); if (c->textalign > TEXTALIGN_RIGHT) { c->textalign = TEXTALIGN_AUTO; } } if (sys->versionlastloaded >= 29) { c->vertalign = dis.Read8(); if (c->vertalign > VERTALIGN_BOTTOM) { c->vertalign = VERTALIGN_AUTO; } } int ts = dis.Read8(); if ((ts & TS_SELECTION_MASK) != 0) { ics = c.get(); ts &= ~TS_SELECTION_MASK; } switch (ts) { case TS_BOTH: case TS_TEXT: c->text.Load(dis); textbytes += c->text.t.Len(); c->SyncFormula(); if (ts == TS_TEXT) { return c.release(); } case TS_GRID: return c->LoadGrid(dis, numcells, textbytes, ics) != nullptr ? c.release() : nullptr; case TS_NEITHER: return c.release(); default: return nullptr; } } unique_ptr Eval(Evaluator &ev) const { // Evaluates the internal grid if it exists, otherwise, evaluate the text. return grid ? grid->Eval(ev) : text.Eval(ev); } // colwidth is the column width original was copied from, which the column of this cell // grows to if it is narrower. void Paste(Document *document, const Cell *original, Selection &selection, int pastemode = PASTE_FIT, int colwidth = 0) { parent->AddUndo(document); ResetLayout(); auto textedit = selection.TextEdit(); if (!HasText() || !textedit) { note = original->note; } if (original->HasText()) { if (!HasText() || !textedit) { cellcolor = original->cellcolor; textcolor = original->textcolor; text.stylebits = original->text.stylebits; } text.Insert(document, original->text.t, selection, false, &original->text); } if (original->formula && !textedit) { // its result as text, and the formula itself celltype = CT_FORMULA; formula = original->formula; } if (original->text.image != nullptr) { text.image = original->text.image; } if (colwidth > 0 && (!original->grid || HasText())) { auto x = parent->grid->FindCell(this).x; if (auto &width = parent->grid->colwidths[x]; width < colwidth) { width = colwidth; parent->grid->ResetResized(Selection(parent->grid, x, 0, 1, parent->grid->ys), false); } } if (original->grid) { shared_ptr gridclone = make_shared(original->grid->xs, original->grid->ys); gridclone->cell = this; original->grid->Clone(gridclone); // The sub-grid keeps its text sizes relative to this cell, which may have taken // another size than the original. if (HasText()) { gridclone->RelSize(text.relsize - original->text.relsize, document->pathscalebias); } // Note: deleting grid may invalidate c if its a child of grid, so clear it. original = nullptr; grid = gridclone; if (!HasText()) { grid->MergeWithParent(parent->grid, selection, document, pastemode); // deletes grid/this. } } } Cell *FindNextSearchMatchStart(const wxString &s, Cell *best, Cell *selected, bool &lastwasselected, bool reverse, bool restricted) { // Skip cell content on starting node if (grid) { best = grid->FindNextSearchMatch(s, best, selected, lastwasselected, reverse, restricted); } return best; } Cell *FindNextSearchMatch(const wxString &s, Cell *best, Cell *selected, bool &lastwasselected, bool reverse, bool restricted) { if (reverse && grid && !(restricted && grid->folded)) { best = grid->FindNextSearchMatch(s, best, selected, lastwasselected, reverse, restricted); } if ((sys->casesensitivesearch ? text.t.Find(s) : text.t.Lower().Find(s)) >= 0) { if (lastwasselected) { best = this; } lastwasselected = false; } if (selected == this) { lastwasselected = true; } if (!reverse && grid && !(restricted && grid->folded)) { best = grid->FindNextSearchMatch(s, best, selected, lastwasselected, reverse, restricted); } return best; } Cell *FindNextFilterMatch(Cell *best, Cell *selected, bool &lastwasselected) { if (!text.filtered) { if (lastwasselected) { best = this; } lastwasselected = false; } if (selected == this) { lastwasselected = true; } if (grid) { best = grid->FindNextFilterMatch(best, selected, lastwasselected); } return best; } Cell *FindLink(const Selection &sel, Cell *link, Cell *best, bool &lastthis, bool &stylematch, bool forward, bool image) { if (grid) { best = grid->FindLink(sel, link, best, lastthis, stylematch, forward, image); } if (link == this) { lastthis = true; return best; } if (image ? link->text.image == text.image : link->text.ToText(0, sel, A_EXPTEXT) == text.t) { if (link->text.stylebits != text.stylebits || link->cellcolor != cellcolor || link->textcolor != textcolor) { if (!stylematch) { best = nullptr; } stylematch = true; } else if (stylematch) { return best; } if (best == nullptr || lastthis) { lastthis = false; return this; } } return best; } void FindReplaceAllStart(const wxString &s, const wxString &ls, bool restricted) { // Skip cell content on starting node if (grid) { grid->FindReplaceAll(s, ls, restricted); } } void FindReplaceAll(const wxString &s, const wxString &ls, bool restricted) { if (grid && !(restricted && grid->folded)) { grid->FindReplaceAll(s, ls, restricted); } text.ReplaceStr(s, ls); } Cell *FindExact(const wxString &s) { return text.t == s ? this : (grid ? grid->FindExact(s) : nullptr); } void ImageRefCount(bool includefolded) { if (grid) { grid->ImageRefCount(includefolded); } if (text.image != nullptr) { text.image->trefc++; } } // Calls f on this cell, and with recursive also on all cells nested inside it. template void ForCells(bool recursive, F f) { f(this); if (recursive && grid) { for (auto &c : grid->cells) c->ForCells(true, f); } } void ColorChange(Document *doc, int which, uint color, bool recursive) { switch (which) { case A_CELLCOLOR: if (IsTag(doc)) { doc->tags[text.t].first = color; } else { cellcolor = color; } break; case A_TEXTCOLOR: if (IsTag(doc)) { doc->tags[text.t].second = color; } else { textcolor = color; text.ClearRunColors(); } break; case A_BORDCOLOR: if (parent != nullptr && parent->grid) { parent->grid->bordercolor = color; } break; } text.WasEdited(); if (recursive && grid) { for (auto &c : grid->cells) c->ColorChange(doc, which, color, true); } } void SetGridTextLayout(int ds, bool vert, bool noset, bool recursive) { if (!noset) { verticaltextandgrid = vert; } if (ds != -1) { drawstyle = ds; } if (recursive && grid) { grid->SetGridTextLayout(ds, vert, noset, grid->SelectAll(), true); } } bool IsTag(Document *doc) { return doc->tags.contains(text.t); } void MaxDepthLeaves(int curdepth, int &maxdepth, int &leaves) { maxdepth = std::max(curdepth, maxdepth); if (grid) { grid->MaxDepthLeaves(curdepth + 1, maxdepth, leaves); } else { leaves++; } } int ColWidth() { return parent != nullptr ? parent->grid->colwidths[parent->grid->FindCell(this).x] : sys->defaultmaxcolwidth; } void CollectCells(vector &itercells, bool recurse = true) { itercells.push_back(this); if (grid && recurse) { grid->CollectCells(itercells); } } Cell *Graph() { auto n = text.GetNum(); text.SetText(wxEmptyString); text.t.Append(L'|', n > 0 ? static_cast(min(n, 1000.0)) : 0); return this; } };