Logo ROOT   6.10/09
Reference Guide
TCanvas.cxx
Go to the documentation of this file.
1 /// \file TCanvas.cxx
2 /// \ingroup Gpad ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-07-10
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
6 
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #include "ROOT/TCanvas.hxx"
17 
18 #include "ROOT/TDrawable.hxx"
19 #include "TCanvas.h"
20 #include "TROOT.h"
21 
22 #include "ROOT/TLogger.hxx"
23 
24 #include <memory>
25 
26 namespace {
27 static
28 std::vector<std::shared_ptr<ROOT::Experimental::TCanvas>>& GetHeldCanvases() {
29  static std::vector<std::shared_ptr<ROOT::Experimental::TCanvas>> sCanvases;
30  return sCanvases;
31 }
32 
33 }
34 
35 namespace ROOT {
36 namespace Experimental {
37 namespace Internal {
38 
39 class TV5CanvasAdaptor: public TObject {
41  ::TCanvas* fOldCanv; // ROOT owns them.
42 
43 public:
44  /// Construct an old TCanvas, append TV5CanvasAdaptor to its primitives.
45  /// That way, TV5CanvasAdaptor::Paint() is called when the TCanvas paints its
46  /// primitives, and TV5CanvasAdaptor::Paint() can forward to
47  /// Experimental::TCanvas::Paint().
48  TV5CanvasAdaptor(ROOT::Experimental::TCanvas& canv):
49  fNewCanv(canv),
50  fOldCanv(new ::TCanvas())
51  {
52  fOldCanv->SetTitle(canv.GetTitle().c_str());
53  AppendPad();
54  }
55 
56  ~TV5CanvasAdaptor() {
57  // Make sure static destruction hasn't already destroyed the old TCanvases.
58  if (gROOT && gROOT->GetListOfCanvases() && !gROOT->GetListOfCanvases()->IsEmpty())
59  fOldCanv->RecursiveRemove(this);
60  }
61 
62  void Paint(Option_t */*option*/="") override {
63  fNewCanv.Paint();
64  }
65 };
66 }
67 }
68 }
69 
70 const std::vector<std::shared_ptr<ROOT::Experimental::TCanvas>> &
72  return GetHeldCanvases();
73 }
74 
75 
77  fAdaptor = std::make_unique<Internal::TV5CanvasAdaptor>(*this);
78 }
79 
81 
83  for (auto&& drw: fPrimitives) {
84  drw->Paint(*this);
85  }
86 }
87 
88 std::shared_ptr<ROOT::Experimental::TCanvas>
89 ROOT::Experimental::TCanvas::Create(const std::string& title) {
90  auto pCanvas = std::make_shared<TCanvas>();
91  pCanvas->SetTitle(title);
92  GetHeldCanvases().emplace_back(pCanvas);
93  return pCanvas;
94 }
95 
96 // TODO: removal from GetHeldCanvases().
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void SetTitle(const char *title="")
Set canvas title.
Definition: TCanvas.cxx:1942
const char Option_t
Definition: RtypesCore.h:62
Graphic container for TDrawable-s.
Definition: TCanvas.hxx:38
#define gROOT
Definition: TROOT.h:375
const std::string & GetTitle() const
Get the canvas&#39;s title.
Definition: TCanvas.hxx:121
virtual void RecursiveRemove(TObject *obj)
Recursively remove object from a pad and its sub-pads.
Definition: TPad.cxx:5143
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:112
TCanvas()
Create a temporary TCanvas; for long-lived ones please use Create().
Definition: TCanvas.cxx:76
~TCanvas()
Default destructor.
The Canvas class.
Definition: TCanvas.h:31
Mother of all ROOT objects.
Definition: TObject.h:37
static std::shared_ptr< TCanvas > Create(const std::string &title)
Definition: TCanvas.cxx:89
void Paint()
Remove an object from the list of primitives.
Definition: TCanvas.cxx:82
static const std::vector< std::shared_ptr< TCanvas > > & GetCanvases()
Definition: TCanvas.cxx:71