Skip to content

Commit

Permalink
Use unique_ptr for Document in TSCanvas (#780)
Browse files Browse the repository at this point in the history
Make it clear that TSCanvas owns the Document.
  • Loading branch information
tobiolo authored Dec 10, 2024
1 parent c7fade2 commit e3d4602
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
7 changes: 2 additions & 5 deletions src/mycanvas.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct TSCanvas : public wxScrolledCanvas {
MyFrame *frame;
Document *doc {nullptr};
unique_ptr<Document> doc;
int mousewheelaccum {0};
bool lastrmbwaswithctrl {false};
wxPoint lastmousepos;
Expand All @@ -17,10 +17,7 @@ struct TSCanvas : public wxScrolledCanvas {
EnableScrolling(false, false);
}

~TSCanvas() {
DELETEP(doc);
frame = nullptr;
}
~TSCanvas() { frame = nullptr; }

void OnPaint(wxPaintEvent &event) {
#if defined(__WXMAC__) || defined(__WXGTK__)
Expand Down
10 changes: 5 additions & 5 deletions src/myframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ struct MyFrame : wxFrame {

TSCanvas *NewTab(Document *doc, bool append = false) {
TSCanvas *sw = new TSCanvas(this, nb);
sw->doc = doc;
sw->doc.reset(doc);
doc->sw = sw;
sw->SetScrollRate(1, 1);
if (append)
Expand Down Expand Up @@ -773,7 +773,7 @@ struct MyFrame : wxFrame {
TSCanvas *sw = (TSCanvas *)nb->GetPage(nbe.GetSelection());
sw->Status();
SetSearchTextBoxBackgroundColour(false);
sys->TabChange(sw->doc);
sys->TabChange(sw->doc.get());
}

void TabsReset() {
Expand Down Expand Up @@ -1082,7 +1082,7 @@ struct MyFrame : wxFrame {
sys->darkennonmatchingcells = searchstring.Len() != 0;
sys->searchstring = (sys->casesensitivesearch) ? searchstring : searchstring.Lower();
SetSearchTextBoxBackgroundColour(false);
Document *doc = GetCurTab()->doc;
Document *doc = GetCurTab()->doc.get();
TSCanvas *sw = GetCurTab();
wxClientDC dc(sw);
doc->SearchNext(dc, false, false, false);
Expand Down Expand Up @@ -1275,7 +1275,7 @@ struct MyFrame : wxFrame {
if ((event.GetChangeType() & 0xF) == 0 || watcherwaitingforuser || !nb) return;
const wxString &modfile = event.GetPath().GetFullPath();
loop(i, nb->GetPageCount()) {
Document *doc = ((TSCanvas *)nb->GetPage(i))->doc;
Document *doc = ((TSCanvas *)nb->GetPage(i))->doc.get();
if (modfile == doc->filename) {
wxDateTime modtime = wxFileName(modfile).GetModificationTime();
// Compare with last modified to trigger multiple times.
Expand Down Expand Up @@ -1307,7 +1307,7 @@ struct MyFrame : wxFrame {
if (*msg) {
GetCurTab()->Status(msg);
} else {
loop(j, nb->GetPageCount()) if (((TSCanvas *)nb->GetPage(j))->doc == doc)
loop(j, nb->GetPageCount()) if (((TSCanvas *)nb->GetPage(j))->doc.get() == doc)
nb->DeletePage(j);
::wxRemoveFile(sys->TmpName(modfile));
GetCurTab()->Status(
Expand Down
2 changes: 1 addition & 1 deletion src/mywxtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct DropTarget : wxDropTarget {
GetData();
TSCanvas *sw = sys->frame->GetCurTab();
sw->SelectClick(x, y, false, 0);
Document *doc = sw->doc;
Document *doc = sw->doc.get();
switch (doc->dndobjc->GetReceivedFormat().GetType()) {
case wxDF_BITMAP: doc->PasteOrDrop(*doc->dndobji); break;
case wxDF_FILENAME: doc->PasteOrDrop(*doc->dndobjf); break;
Expand Down
2 changes: 1 addition & 1 deletion src/treesheets_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct TreeSheetsScriptImpl : public ScriptInterface {
enum { max_new_grid_cells = 256 * 256 }; // Don't allow crazy sizes.

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

doc->AddUndo(cur);
Expand Down

0 comments on commit e3d4602

Please sign in to comment.