Skip to content

Commit 1060b14

Browse files
committed
Use TWebCanvas::CreateWebCanvas in RBrowser
Simplify code, no need to access private TCanvas fields via workaround
1 parent 4e177b9 commit 1060b14

File tree

1 file changed

+28
-86
lines changed

1 file changed

+28
-86
lines changed

gui/browserv7/src/RBrowserTCanvasWidget.cxx

Lines changed: 28 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -48,120 +48,72 @@ class RBrowserTCanvasWidget : public RBrowserWidget {
4848
return false;
4949
}
5050

51-
void SetPrivateCanvasFields(bool on_init)
52-
{
53-
Long_t offset = TCanvas::Class()->GetDataMemberOffset("fCanvasID");
54-
if (offset > 0) {
55-
Int_t *id = (Int_t *)((char *) fCanvas + offset);
56-
if (*id == fCanvas->GetCanvasID()) *id = on_init ? 111222333 : -1;
57-
} else {
58-
printf("ERROR: Cannot modify TCanvas::fCanvasID data member\n");
59-
}
60-
61-
offset = TCanvas::Class()->GetDataMemberOffset("fPixmapID");
62-
if (offset > 0) {
63-
Int_t *id = (Int_t *)((char *) fCanvas + offset);
64-
if (*id == fCanvas->GetPixmapID()) *id = on_init ? 332211 : -1;
65-
} else {
66-
printf("ERROR: Cannot modify TCanvas::fPixmapID data member\n");
67-
}
68-
69-
offset = TCanvas::Class()->GetDataMemberOffset("fMother");
70-
if (offset > 0) {
71-
TPad **moth = (TPad **)((char *) fCanvas + offset);
72-
if (*moth == fCanvas->GetMother()) *moth = on_init ? fCanvas : nullptr;
73-
} else {
74-
printf("ERROR: Cannot set TCanvas::fMother data member\n");
75-
}
76-
77-
offset = TCanvas::Class()->GetDataMemberOffset("fCw");
78-
if (offset > 0) {
79-
UInt_t *cw = (UInt_t *)((char *) fCanvas + offset);
80-
if (*cw == fCanvas->GetWw()) *cw = on_init ? 800 : 0;
81-
} else {
82-
printf("ERROR: Cannot set TCanvas::fCw data member\n");
83-
}
84-
85-
offset = TCanvas::Class()->GetDataMemberOffset("fCh");
86-
if (offset > 0) {
87-
UInt_t *ch = (UInt_t *)((char *) fCanvas + offset);
88-
if (*ch == fCanvas->GetWh()) *ch = on_init ? 600 : 0;
89-
} else {
90-
printf("ERROR: Cannot set TCanvas::fCw data member\n");
91-
}
92-
}
93-
94-
void RegisterCanvasInGlobalLists()
51+
void RegisterCanvasInGlobalLists(bool add_canvas)
9552
{
9653
R__LOCKGUARD(gROOTMutex);
9754
auto l1 = gROOT->GetListOfCleanups();
98-
if (!l1->FindObject(fCanvas))
99-
l1->Add(fCanvas);
55+
if (l1) {
56+
if (!add_canvas)
57+
l1->Remove(fCanvas);
58+
else if (!l1->FindObject(fCanvas))
59+
l1->Add(fCanvas);
60+
}
10061
auto l2 = gROOT->GetListOfCanvases();
101-
if (!l2->FindObject(fCanvas))
102-
l2->Add(fCanvas);
62+
if (l2) {
63+
if (!add_canvas)
64+
l2->Remove(fCanvas);
65+
else if (!l2->FindObject(fCanvas))
66+
l2->Add(fCanvas);
67+
}
10368
}
10469

10570
public:
10671

72+
// constructor when new canvas should be created
10773
RBrowserTCanvasWidget(const std::string &name) : RBrowserWidget(name)
10874
{
10975
fCanvasName = name.c_str();
11076

111-
fCanvas = new TCanvas(kFALSE);
112-
fCanvas->SetName(fCanvasName);
113-
fCanvas->SetTitle(fCanvasName);
77+
// create canvas with web display
78+
fCanvas = TWebCanvas::CreateWebCanvas(fCanvasName, fCanvasName);
79+
11480
fCanvas->ResetBit(TCanvas::kShowEditor);
11581
fCanvas->ResetBit(TCanvas::kShowToolBar);
11682
fCanvas->SetBit(TCanvas::kMenuBar, kTRUE);
117-
fCanvas->SetCanvas(fCanvas);
118-
fCanvas->SetBatch(kTRUE); // mark canvas as batch
119-
fCanvas->SetEditable(kTRUE); // ensure fPrimitives are created
12083

121-
Bool_t readonly = gEnv->GetValue("WebGui.FullCanvas", (Int_t) 1) == 0;
122-
123-
// create implementation
124-
fWebCanvas = new TWebCanvas(fCanvas, "title", 0, 0, 800, 600, readonly);
84+
// get implementation
85+
fWebCanvas = static_cast<TWebCanvas *> (fCanvas->GetCanvasImp());
12586

12687
// use async mode to prevent blocking inside qt5/qt6/cef
12788
fWebCanvas->SetAsyncMode(kTRUE);
12889

129-
// assign implementation
130-
fCanvas->SetCanvasImp(fWebCanvas);
131-
SetPrivateCanvasFields(true);
132-
fCanvas->cd();
133-
134-
RegisterCanvasInGlobalLists();
135-
136-
// ensure creation of web window
137-
fWebCanvas->ShowWebWindow("embed");
90+
RegisterCanvasInGlobalLists(true);
13891
}
13992

93+
// constructor when widget for existing canvas should be created
14094
RBrowserTCanvasWidget(const std::string &name, std::unique_ptr<TCanvas> &canv) : RBrowserWidget(name)
14195
{
14296
fCanvas = canv.release();
14397
fCanvasName = fCanvas->GetName();
14498
fCanvas->SetBatch(kTRUE); // mark canvas as batch
14599

146-
Bool_t readonly = gEnv->GetValue("WebGui.FullCanvas", (Int_t) 1) == 0;
147-
148100
// create implementation
149-
fWebCanvas = new TWebCanvas(fCanvas, "title", 0, 0, 800, 600, readonly);
101+
fWebCanvas = static_cast<TWebCanvas *> (TWebCanvas::NewCanvas(fCanvas, "title", 0, 0, 800, 600));
150102

151103
// use async mode to prevent blocking inside qt5/qt6/cef
152104
fWebCanvas->SetAsyncMode(kTRUE);
153105

154106
// assign implementation
155107
fCanvas->SetCanvasImp(fWebCanvas);
156-
SetPrivateCanvasFields(true);
157108
fCanvas->cd();
158109

159-
RegisterCanvasInGlobalLists();
160-
161110
// ensure creation of web window
162-
fWebCanvas->ShowWebWindow("embed");
111+
fWebCanvas->CreateWebWindow();
112+
113+
RegisterCanvasInGlobalLists(true);
163114
}
164115

116+
// constructor when canvas already displayed and just integrated into RBrowser
165117
RBrowserTCanvasWidget(const std::string &name, TCanvas *canv, TWebCanvas *webcanv) : RBrowserWidget(name)
166118
{
167119
fCanvas = canv;
@@ -171,26 +123,16 @@ class RBrowserTCanvasWidget : public RBrowserWidget {
171123
fWebCanvas = webcanv;
172124
// use async mode to prevent blocking inside qt5/qt6/cef
173125
fWebCanvas->SetAsyncMode(kTRUE);
174-
175-
// assign implementation
176-
SetPrivateCanvasFields(true);
177126
}
178127

179128
virtual ~RBrowserTCanvasWidget()
180129
{
181130
if (!fCanvas || !gROOT->GetListOfCanvases()->FindObject(fCanvas))
182131
return;
183132

184-
{
185-
R__LOCKGUARD(gROOTMutex);
186-
gROOT->GetListOfCleanups()->Remove(fCanvas);
187-
}
188-
189-
SetPrivateCanvasFields(false);
190-
191-
gROOT->GetListOfCanvases()->Remove(fCanvas);
133+
RegisterCanvasInGlobalLists(false);
192134

193-
if ((fCanvas->GetCanvasID() == -1) && (fCanvas->GetCanvasImp() == fWebCanvas)) {
135+
if (fCanvas->GetCanvasImp() == fWebCanvas) {
194136
fCanvas->SetCanvasImp(nullptr);
195137
delete fWebCanvas;
196138
}

0 commit comments

Comments
 (0)