From 3ac1d584c821d21e4fae789f595eddf5833848c0 Mon Sep 17 00:00:00 2001
From: Sergey Linev <S.Linev@gsi.de>
Date: Thu, 1 Dec 2022 14:27:55 +0100
Subject: [PATCH] Introduce plugin handler for TWebCanvas

Allows to create web-based canvas from regular TGuiFactory
So also in batch mode one should be able to start web-based widgets
---
 core/gui/src/TGuiFactory.cxx             | 18 ++++++++++++++++++
 etc/plugins/TCanvasImp/P010_TWebCanvas.C |  5 +++++
 gui/webgui6/inc/TWebCanvas.h             |  2 ++
 gui/webgui6/src/TWebCanvas.cxx           | 10 ++++++++++
 4 files changed, 35 insertions(+)
 create mode 100644 etc/plugins/TCanvasImp/P010_TWebCanvas.C

diff --git a/core/gui/src/TGuiFactory.cxx b/core/gui/src/TGuiFactory.cxx
index ea8a9ef075e..7767b413770 100644
--- a/core/gui/src/TGuiFactory.cxx
+++ b/core/gui/src/TGuiFactory.cxx
@@ -57,6 +57,15 @@ TApplicationImp *TGuiFactory::CreateApplicationImp(const char *classname, int *a
 
 TCanvasImp *TGuiFactory::CreateCanvasImp(TCanvas *c, const char *title, UInt_t width, UInt_t height)
 {
+   if (gROOT->IsWebDisplay()) {
+      auto ph = gROOT->GetPluginManager()->FindHandler("TCanvasImp", "TWebCanvas");
+
+      if (ph && ph->LoadPlugin() != -1) {
+         auto imp = (TCanvasImp *) ph->ExecPlugin(6, c, title, 0, 0, width, height);
+         if (imp) return imp;
+      }
+   }
+
    return new TCanvasImp(c, title, width, height);
 }
 
@@ -65,6 +74,15 @@ TCanvasImp *TGuiFactory::CreateCanvasImp(TCanvas *c, const char *title, UInt_t w
 
 TCanvasImp *TGuiFactory::CreateCanvasImp(TCanvas *c, const char *title, Int_t x, Int_t y, UInt_t width, UInt_t height)
 {
+   if (gROOT->IsWebDisplay()) {
+       auto ph = gROOT->GetPluginManager()->FindHandler("TCanvasImp", "TWebCanvas");
+
+       if (ph && ph->LoadPlugin() != -1) {
+          auto imp = (TCanvasImp *) ph->ExecPlugin(6, c, title, x, y, width, height);
+          if (imp) return imp;
+       }
+    }
+
    return new TCanvasImp(c, title, x, y, width, height);
 }
 
diff --git a/etc/plugins/TCanvasImp/P010_TWebCanvas.C b/etc/plugins/TCanvasImp/P010_TWebCanvas.C
new file mode 100644
index 00000000000..4ba017e3b2e
--- /dev/null
+++ b/etc/plugins/TCanvasImp/P010_TWebCanvas.C
@@ -0,0 +1,5 @@
+void P010_TWebCanvas()
+{
+   gPluginMgr->AddHandler("TCanvasImp", "TWebCanvas", "TWebCanvas",
+      "WebGui6", "NewCanvas(TCanvas *, const char *, Int_t, Int_t, UInt_t, UInt_t)");
+}
diff --git a/gui/webgui6/inc/TWebCanvas.h b/gui/webgui6/inc/TWebCanvas.h
index 5acc29876d3..5bb6cd96dfc 100644
--- a/gui/webgui6/inc/TWebCanvas.h
+++ b/gui/webgui6/inc/TWebCanvas.h
@@ -229,6 +229,8 @@ public:
 
    static bool ProduceImage(TCanvas *c, const char *filename, Int_t width = 0, Int_t height = 0);
 
+   static TCanvasImp *NewCanvas(TCanvas *c, const char *name, Int_t x, Int_t y, UInt_t width, UInt_t height);
+
    ClassDefOverride(TWebCanvas, 0) // Web-based implementation for TCanvasImp, read-only mode
 };
 
diff --git a/gui/webgui6/src/TWebCanvas.cxx b/gui/webgui6/src/TWebCanvas.cxx
index 38ad1883e68..2b0e9f8b054 100644
--- a/gui/webgui6/src/TWebCanvas.cxx
+++ b/gui/webgui6/src/TWebCanvas.cxx
@@ -1876,4 +1876,14 @@ TObject *TWebCanvas::FindPrimitive(const std::string &sid, int idcnt, TPad *pad,
    return nullptr;
 }
 
+//////////////////////////////////////////////////////////////////////////////////////////////////
+/// Static method to create TWebCanvas instance
+/// Used by plugin manager
+
+TCanvasImp *TWebCanvas::NewCanvas(TCanvas *c, const char *name, Int_t x, Int_t y, UInt_t width, UInt_t height)
+{
+   Bool_t readonly = gEnv->GetValue("WebGui.FullCanvas", (Int_t) 1) == 0;
+
+   return new TWebCanvas(c, name, x, y, width, height, readonly);
+}
 
-- 
GitLab