Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TPaveLabel.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Rene Brun 17/10/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <iostream>
13#include "TROOT.h"
14#include "TStyle.h"
15#include "TPaveLabel.h"
16#include "TLatex.h"
17#include "TVirtualPad.h"
18
20
21/** \class TPaveLabel
22\ingroup BasicGraphics
23
24A Pave (see TPave) with a text centered in the Pave.
25
26\image html graf_pavelabel.png
27*/
28
29////////////////////////////////////////////////////////////////////////////////
30/// Pavelabel default constructor.
31
33{
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// Pavelabel normal constructor.
38///
39/// a PaveLabel is a Pave with a label centered in the Pave
40/// The Pave is by default defined bith bordersize=5 and option ="br".
41/// The text size is automatically computed as a function of the pave size.
42/// To remove the shadow or border of a TPaveLabel, use the function TPave::SetBorderSize
43
45 :TPave(x1,y1,x2,y2,3,option), TAttText(22,0,1,gStyle->GetTextFont(),0.99)
46{
47 fLabel = label;
48}
49
50////////////////////////////////////////////////////////////////////////////////
51/// TPaveLabel default destructor.
52
54{
55}
56
57////////////////////////////////////////////////////////////////////////////////
58/// TPaveLabel copy constructor.
59
60TPaveLabel::TPaveLabel(const TPaveLabel &pavelabel) : TPave(pavelabel), TAttText(pavelabel)
61{
62 pavelabel.TPaveLabel::Copy(*this);
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// TPaveLabel assign operator
67
69{
70 if (this != &pavelabel)
71 pavelabel.TPaveLabel::Copy(*this);
72 return *this;
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Copy this pavelabel to pavelabel.
77
78void TPaveLabel::Copy(TObject &obj) const
79{
80 TPave::Copy(obj);
82 ((TPaveLabel &)obj).fLabel = fLabel;
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Draw this pavelabel with its current attributes.
87
89{
90 Option_t *opt;
91 if (option && *option) opt = option;
92 else opt = GetOption();
93
94 AppendPad(opt);
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Draw this pavelabel with new coordinates.
99
101{
102 TPaveLabel *newpavelabel = new TPaveLabel(x1,y1,x2,y2,label,option);
103 newpavelabel->SetBit(kCanDelete);
104 newpavelabel->AppendPad();
105 return newpavelabel;
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// Paint this pavelabel with its current attributes.
110
112{
113 // Convert from NDC to pad coordinates
115
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Draw this pavelabel with new coordinates.
121
123 const char *label ,Option_t *option)
124{
125 if (!gPad) return;
126 Int_t nch = label ? strlen(label) : 0;
127
128 // Draw the pave
130
131 Float_t nspecials = 0;
132 for (Int_t i=0;i<nch;i++) {
133 if (label[i] == '!') nspecials += 1;
134 if (label[i] == '?') nspecials += 1.5;
135 if (label[i] == '#') nspecials += 1;
136 if (label[i] == '`') nspecials += 1;
137 if (label[i] == '^') nspecials += 1.5;
138 if (label[i] == '~') nspecials += 1;
139 if (label[i] == '&') nspecials += 2;
140 if (label[i] == '\\') nspecials += 3; // octal characters very likely
141 }
142 nch -= Int_t(nspecials + 0.5);
143 if (nch <= 0) return;
144
145 // Draw label
146 Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
147 Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
148 if (wh==0||hh==0) return;
149 Double_t labelsize, textsize = GetTextSize();
150 Int_t automat = 0;
151 if (GetTextFont()%10 > 2) { // fixed size font specified in pixels
152 labelsize = GetTextSize();
153 } else {
154 if (TMath::Abs(textsize -0.99) < 0.001) automat = 1;
155 if (textsize == 0) { textsize = 0.99; automat = 1;}
156 Int_t ypixel = TMath::Abs(gPad->YtoPixel(y1) - gPad->YtoPixel(y2));
157 labelsize = textsize*ypixel/hh;
158 if (wh < hh) labelsize *= hh/wh;
159 }
160 TLatex latex;
161 latex.SetTextAngle(GetTextAngle());
162 latex.SetTextFont(GetTextFont());
163 latex.SetTextAlign(GetTextAlign());
164 latex.SetTextColor(GetTextColor());
165 latex.SetTextSize(labelsize);
166 if (automat) {
167 UInt_t w=0,h=0,w1=0;
168 latex.GetTextExtent(w,h,GetTitle());
169 if (!w) return;
170 labelsize = h/hh;
171 Double_t wxlabel = TMath::Abs(gPad->XtoPixel(x2) - gPad->XtoPixel(x1));
172 latex.GetTextExtent(w1,h,GetTitle());
173 while (w > 0.99*wxlabel) {
174 labelsize *= 0.99*wxlabel/w;
175 latex.SetTextSize(labelsize);
176 latex.GetTextExtent(w,h,GetTitle());
177 if (w==w1) break;
178 else w1=w;
179 }
180 if (h < 1) h = 1;
181 if (h==1) {
182 labelsize = Double_t(h)/hh;
183 if (wh < hh) labelsize *= hh/wh;
184 latex.SetTextSize(labelsize);
185 }
186 }
187 Int_t halign = GetTextAlign()/10;
188 Int_t valign = GetTextAlign()%10;
189 Double_t x = 0.5*(x1+x2);
190 if (halign == 1) x = x1 + 0.02*(x2-x1);
191 if (halign == 3) x = x2 - 0.02*(x2-x1);
192 Double_t y = 0.5*(y1+y2);
193 if (valign == 1) y = y1 + 0.02*(y2-y1);
194 if (valign == 3) y = y2 - 0.02*(y2-y1);
195 latex.PaintLatex(x, y, GetTextAngle(),labelsize,GetLabel());
196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Save primitive as a C++ statement(s) on output stream out
200
201void TPaveLabel::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
202{
203 char quote = '"';
204 out<<" "<<std::endl;
205 if (gROOT->ClassSaved(TPaveLabel::Class())) {
206 out<<" ";
207 } else {
208 out<<" TPaveLabel *";
209 }
210 TString s = fLabel.Data();
211 s.ReplaceAll("\"","\\\"");
212 if (fOption.Contains("NDC")) {
213 out<<"pl = new TPaveLabel("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
214 <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
215 } else {
216 if (gPad) {
217 out<<"pl = new TPaveLabel("<<gPad->PadtoX(fX1)<<","<<gPad->PadtoY(fY1)<<","<<gPad->PadtoX(fX2)<<","<<gPad->PadtoY(fY2)
218 <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
219 }
220 }
221 if (fBorderSize != 3) {
222 out<<" pl->SetBorderSize("<<fBorderSize<<");"<<std::endl;
223 }
224 SaveFillAttributes(out,"pl",19,1001);
225 SaveLineAttributes(out,"pl",1,1,1);
226 SaveTextAttributes(out,"pl",22,0,1,62,0);
227
228 out<<" pl->Draw();"<<std::endl;
229}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t option
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t textsize
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition TAttFill.cxx:239
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition TAttLine.cxx:275
Text Attributes class.
Definition TAttText.h:18
virtual Float_t GetTextSize() const
Return the text size.
Definition TAttText.h:36
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:42
virtual Short_t GetTextAlign() const
Return the text alignment.
Definition TAttText.h:32
virtual Font_t GetTextFont() const
Return the text font.
Definition TAttText.h:35
virtual Color_t GetTextColor() const
Return the text color.
Definition TAttText.h:34
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition TAttText.h:43
virtual Float_t GetTextAngle() const
Return the text angle.
Definition TAttText.h:33
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
virtual void SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef=12, Float_t angdef=0, Int_t coldef=1, Int_t fondef=61, Float_t sizdef=1)
Save text attributes as C++ statement(s) on output stream out.
Definition TAttText.cxx:373
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
void Copy(TAttText &atttext) const
Copy this text attributes to a new TAttText.
Definition TAttText.cxx:294
Double_t fX1
X of 1st point.
Definition TBox.h:28
Double_t fY2
Y of 2nd point.
Definition TBox.h:31
Double_t fX2
X of 2nd point.
Definition TBox.h:30
Double_t fY1
Y of 1st point.
Definition TBox.h:29
To draw Mathematical Formula.
Definition TLatex.h:18
virtual void PaintLatex(Double_t x, Double_t y, Double_t angle, Double_t size, const char *text)
Main drawing function.
Definition TLatex.cxx:2114
Mother of all ROOT objects.
Definition TObject.h:41
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:190
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:786
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:62
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
void Copy(TObject &pavelabel) const override
Copy this pavelabel to pavelabel.
virtual void PaintPaveLabel(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char *label, Option_t *option="")
Draw this pavelabel with new coordinates.
~TPaveLabel() override
TPaveLabel default destructor.
static TClass * Class()
virtual TPaveLabel * DrawPaveLabel(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char *label, Option_t *option="")
Draw this pavelabel with new coordinates.
const char * GetLabel() const
Definition TPaveLabel.h:36
void Paint(Option_t *option="") override
Paint this pavelabel with its current attributes.
TPaveLabel()
Pavelabel default constructor.
TString fLabel
Label written at the center of Pave.
Definition TPaveLabel.h:23
TPaveLabel & operator=(const TPaveLabel &pavelabel)
TPaveLabel assign operator.
void Draw(Option_t *option="") override
Draw this pavelabel with its current attributes.
const char * GetTitle() const override
Returns title of object.
Definition TPaveLabel.h:37
A TBox with a bordersize and a shadow option.
Definition TPave.h:19
Int_t GetBorderSize() const
Definition TPave.h:54
virtual void ConvertNDCtoPad()
Convert pave coordinates from NDC to Pad coordinates.
Definition TPave.cxx:139
void Copy(TObject &pave) const override
Copy this pave to pave.
Definition TPave.cxx:186
Int_t fBorderSize
window box bordersize in pixels
Definition TPave.h:26
Double_t fX2NDC
X2 point in NDC coordinates.
Definition TPave.h:24
TString fOption
Pave style.
Definition TPave.h:30
Double_t fY2NDC
Y2 point in NDC coordinates.
Definition TPave.h:25
Double_t fX1NDC
X1 point in NDC coordinates.
Definition TPave.h:22
Option_t * GetOption() const override
Definition TPave.h:57
Double_t fY1NDC
Y1 point in NDC coordinates.
Definition TPave.h:23
virtual void PaintPave(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize=4, Option_t *option="br")
Draw this pave with new coordinates.
Definition TPave.cxx:315
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
virtual void GetTextExtent(UInt_t &w, UInt_t &h, const char *text) const
Return text extent for string text.
Definition TText.cxx:591
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123