Skip to content

Commit

Permalink
Make rootgrid an unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiolo committed Nov 26, 2024
1 parent 4b27fa4 commit 50e307c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
22 changes: 10 additions & 12 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct UndoItem {

struct Document {
TSCanvas *sw {nullptr};
Cell *rootgrid {nullptr};
unique_ptr<Cell> rootgrid;
Selection hover;
Selection selected;
Selection begindrag;
Expand Down Expand Up @@ -91,7 +91,7 @@ struct Document {
CollectCells(par); \
loopv(_i, itercells) for (Cell *c = itercells[_i]; c; c = nullptr)
#define loopallcells(c) \
CollectCells(rootgrid); \
CollectCells(rootgrid.get()); \
for (Cell *c : itercells)
#define loopallcellssel(c, rec) \
CollectCellsSel(rec); \
Expand All @@ -107,8 +107,6 @@ struct Document {
dndobjc->Add(dndobjf);
}

~Document() { DELETEP(rootgrid); }

uint Background() { return rootgrid ? rootgrid->cellcolor : 0xFFFFFF; }

void InitCellSelect(Cell *ics, int xs, int ys) {
Expand All @@ -127,7 +125,7 @@ struct Document {
}

void InitWith(Cell *r, const wxString &filename, Cell *ics, int xs, int ys) {
rootgrid = r;
rootgrid.reset(r);
InitCellSelect(ics, xs, ys);
ChangeFileName(filename, false);
}
Expand Down Expand Up @@ -570,7 +568,7 @@ struct Document {
ResetFont();
dc.SetUserScale(1, 1);
curdrawroot = WalkPath(drawpath);
int psb = curdrawroot == rootgrid ? 0 : curdrawroot->MinRelsize();
int psb = curdrawroot == rootgrid.get() ? 0 : curdrawroot->MinRelsize();
if (psb < 0 || psb == INT_MAX) psb = 0;
if (psb != pathscalebias) curdrawroot->ResetChildren();
pathscalebias = psb;
Expand Down Expand Up @@ -813,7 +811,7 @@ struct Document {
}

const wxChar *ExportFile(const wxString &fn, int k, bool currentview) {
auto root = currentview ? curdrawroot : rootgrid;
auto root = currentview ? curdrawroot : rootgrid.get();
if (k == A_EXPCSV) {
int maxdepth = 0, leaves = 0;
root->MaxDepthLeaves(0, maxdepth, leaves);
Expand Down Expand Up @@ -978,7 +976,7 @@ struct Document {

switch (k) {
case wxID_EXECUTE:
sys->ev.Eval(rootgrid);
sys->ev.Eval(rootgrid.get());
rootgrid->ResetChildren();
ClearSelectionRefresh();
return _(L"Evaluation finished.");
Expand Down Expand Up @@ -2147,7 +2145,7 @@ struct Document {
}

Cell *WalkPath(vector<Selection> &path) {
Cell *c = rootgrid;
Cell *c = rootgrid.get();
loopvrev(i, path) {
Selection &s = path[i];
Grid *g = c->grid;
Expand Down Expand Up @@ -2218,7 +2216,7 @@ struct Document {
c->parent->grid->ReplaceCell(c, clone);
clone->parent = c->parent;
} else
rootgrid = clone;
rootgrid.reset(clone);
clone->ResetLayout();
SetSelect(ui->sel);
if (selected.g) selected.g = WalkPath(ui->selpath)->grid;
Expand Down Expand Up @@ -2305,7 +2303,7 @@ struct Document {
searchfilter = false;
scrolltoselection = true;
editfilter = min(max(editfilter, 1), 99);
CollectCells(rootgrid);
CollectCells(rootgrid.get());
std::sort(itercells.begin(), itercells.end(), [](Cell *a, Cell *b) {
// sort in descending order
return a->text.lastedit > b->text.lastedit;
Expand All @@ -2318,7 +2316,7 @@ struct Document {
void ApplyEditRangeFilter(wxDateTime &rangebegin, wxDateTime &rangeend) {
searchfilter = false;
scrolltoselection = true;
CollectCells(rootgrid);
CollectCells(rootgrid.get());
for (auto *c : itercells) {
c->text.filtered = !c->text.lastedit.IsBetween(rangebegin, rangeend);
}
Expand Down
2 changes: 1 addition & 1 deletion src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ struct Grid {
const int root_grid_spacing = 2; // Can't be adjusted in editor, so use a default.
const int font_size = 14 - indent / 2;
const int grid_border_width =
cell == doc->rootgrid ? root_grid_spacing : user_grid_outer_spacing - 1;
cell == doc->rootgrid.get() ? root_grid_spacing : user_grid_outer_spacing - 1;

wxString xmlstr(L"<grid");
if (folded) xmlstr.Append(wxString::Format(wxT(" folded=\"%d\""), folded));
Expand Down
9 changes: 4 additions & 5 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ struct System {

void LoadOpRef() { LoadDB(frame->GetDocPath(L"examples/operation-reference.cts")); }

Cell *&InitDB(int sizex, int sizey = 0) {
unique_ptr<Cell> &InitDB(int sizex, int sizey = 0) {
Cell *c = new Cell(nullptr, nullptr, CT_DATA, new Grid(sizex, sizey ? sizey : sizex));
c->cellcolor = 0xCCDCE2;
c->grid->InitCells();
Expand Down Expand Up @@ -464,13 +464,12 @@ struct System {
case A_IMPXMLA: {
wxXmlDocument doc;
if (!doc.Load(fn)) goto problem;
Cell *&r = InitDB(1);
unique_ptr<Cell> &r = InitDB(1);
Cell *c = *r->grid->cells;
FillXML(c, doc.GetRoot(), k == A_IMPXMLA);
if (!c->HasText() && c->grid) {
*r->grid->cells = nullptr;
delete r;
r = c;
r.reset(c);
c->parent = nullptr;
}
break;
Expand All @@ -487,7 +486,7 @@ struct System {

if (as.size()) switch (k) {
case A_IMPTXTI: {
Cell *r = InitDB(1);
Cell *r = InitDB(1).get();
FillRows(r->grid, as, CountCol(as[0]), 0, 0);
}; break;
case A_IMPTXTC:
Expand Down
4 changes: 2 additions & 2 deletions src/treesheets_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct TreeSheetsScriptImpl : public ScriptInterface {

void SwitchToCurrentDoc() {
doc = sys->frame->GetCurTab()->doc;
cur = doc->rootgrid;
cur = doc->rootgrid.get();

doc->AddUndo(cur);
}
Expand Down Expand Up @@ -42,7 +42,7 @@ struct TreeSheetsScriptImpl : public ScriptInterface {
return true;
}

void GoToRoot() { cur = doc->rootgrid; }
void GoToRoot() { cur = doc->rootgrid.get(); }
void GoToView() { cur = doc->curdrawroot; }
bool HasSelection() { return doc->selected.g; }
void GoToSelection() {
Expand Down

0 comments on commit 50e307c

Please sign in to comment.