Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 "Riostream.h"
13 #include "TROOT.h"
14 #include "TStyle.h"
15 #include "TPaveLabel.h"
16 #include "TLatex.h"
17 #include "TVirtualPad.h"
18 
19 ClassImp(TPaveLabel);
20 
21 /** \class TPaveLabel
22 \ingroup BasicGraphics
23 
24 A 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 
32 TPaveLabel::TPaveLabel(): TPave(), TAttText()
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 
43 TPaveLabel::TPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t y2, const char *label, Option_t *option)
44  :TPave(x1,y1,x2,y2,3,option), TAttText(22,0,1,gStyle->GetTextFont(),0.99)
45 {
46  fLabel = label;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Pavelabel default destructor.
51 
52 TPaveLabel::~TPaveLabel()
53 {
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Pavelabel copy constructor.
58 
59 TPaveLabel::TPaveLabel(const TPaveLabel &pavelabel) : TPave(pavelabel), TAttText(pavelabel)
60 {
61  ((TPaveLabel&)pavelabel).Copy(*this);
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Copy this pavelabel to pavelabel.
66 
67 void TPaveLabel::Copy(TObject &obj) const
68 {
69  TPave::Copy(obj);
70  TAttText::Copy(((TPaveLabel&)obj));
71  ((TPaveLabel&)obj).fLabel = fLabel;
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Draw this pavelabel with its current attributes.
76 
77 void TPaveLabel::Draw(Option_t *option)
78 {
79  Option_t *opt;
80  if (option && strlen(option)) opt = option;
81  else opt = GetOption();
82 
83  AppendPad(opt);
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Draw this pavelabel with new coordinates.
88 
89 void TPaveLabel::DrawPaveLabel(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char *label, Option_t *option)
90 {
91  TPaveLabel *newpavelabel = new TPaveLabel(x1,y1,x2,y2,label,option);
92  newpavelabel->SetBit(kCanDelete);
93  newpavelabel->AppendPad();
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Paint this pavelabel with its current attributes.
98 
99 void TPaveLabel::Paint(Option_t *option)
100 {
101  // Convert from NDC to pad coordinates
102  TPave::ConvertNDCtoPad();
103 
104  PaintPaveLabel(fX1, fY1, fX2, fY2, GetLabel(), strlen(option)?option:GetOption());
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Draw this pavelabel with new coordinates.
109 
110 void TPaveLabel::PaintPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t y2,
111  const char *label ,Option_t *option)
112 {
113  Int_t nch = strlen(label);
114 
115  // Draw the pave
116  TPave::PaintPave(x1,y1,x2,y2,GetBorderSize(),option);
117 
118  Float_t nspecials = 0;
119  for (Int_t i=0;i<nch;i++) {
120  if (label[i] == '!') nspecials += 1;
121  if (label[i] == '?') nspecials += 1.5;
122  if (label[i] == '#') nspecials += 1;
123  if (label[i] == '`') nspecials += 1;
124  if (label[i] == '^') nspecials += 1.5;
125  if (label[i] == '~') nspecials += 1;
126  if (label[i] == '&') nspecials += 2;
127  if (label[i] == '\\') nspecials += 3; // octal characters very likely
128  }
129  nch -= Int_t(nspecials + 0.5);
130  if (nch <= 0) return;
131 
132  // Draw label
133  Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
134  Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
135  Double_t labelsize, textsize = GetTextSize();
136  Int_t automat = 0;
137  if (GetTextFont()%10 > 2) { // fixed size font specified in pixels
138  labelsize = GetTextSize();
139  } else {
140  if (TMath::Abs(textsize -0.99) < 0.001) automat = 1;
141  if (textsize == 0) { textsize = 0.99; automat = 1;}
142  Int_t ypixel = TMath::Abs(gPad->YtoPixel(y1) - gPad->YtoPixel(y2));
143  labelsize = textsize*ypixel/hh;
144  if (wh < hh) labelsize *= hh/wh;
145  }
146  TLatex latex;
147  latex.SetTextAngle(GetTextAngle());
148  latex.SetTextFont(GetTextFont());
149  latex.SetTextAlign(GetTextAlign());
150  latex.SetTextColor(GetTextColor());
151  latex.SetTextSize(labelsize);
152  if (automat) {
153  UInt_t w=0,h=0,w1=0;
154  latex.GetTextExtent(w,h,GetTitle());
155  if (!w) return;
156  labelsize = h/hh;
157  Double_t wxlabel = TMath::Abs(gPad->XtoPixel(x2) - gPad->XtoPixel(x1));
158  latex.GetTextExtent(w1,h,GetTitle());
159  while (w > 0.99*wxlabel) {
160  labelsize *= 0.99*wxlabel/w;
161  latex.SetTextSize(labelsize);
162  latex.GetTextExtent(w,h,GetTitle());
163  if (w==w1) break;
164  else w1=w;
165  }
166  if (h < 1) h = 1;
167  if (h==1) {
168  labelsize = Double_t(h)/hh;
169  if (wh < hh) labelsize *= hh/wh;
170  latex.SetTextSize(labelsize);
171  }
172  }
173  Int_t halign = GetTextAlign()/10;
174  Int_t valign = GetTextAlign()%10;
175  Double_t x = 0.5*(x1+x2);
176  if (halign == 1) x = x1 + 0.02*(x2-x1);
177  if (halign == 3) x = x2 - 0.02*(x2-x1);
178  Double_t y = 0.5*(y1+y2);
179  if (valign == 1) y = y1 + 0.02*(y2-y1);
180  if (valign == 3) y = y2 - 0.02*(y2-y1);
181  latex.PaintLatex(x, y, GetTextAngle(),labelsize,GetLabel());
182 }
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 /// Save primitive as a C++ statement(s) on output stream out
186 
187 void TPaveLabel::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
188 {
189  char quote = '"';
190  out<<" "<<std::endl;
191  if (gROOT->ClassSaved(TPaveLabel::Class())) {
192  out<<" ";
193  } else {
194  out<<" TPaveLabel *";
195  }
196  TString s = fLabel.Data();
197  s.ReplaceAll("\"","\\\"");
198  if (fOption.Contains("NDC")) {
199  out<<"pl = new TPaveLabel("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
200  <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
201  } else {
202  out<<"pl = new TPaveLabel("<<gPad->PadtoX(fX1)<<","<<gPad->PadtoY(fY1)<<","<<gPad->PadtoX(fX2)<<","<<gPad->PadtoY(fY2)
203  <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
204  }
205  if (fBorderSize != 3) {
206  out<<" pl->SetBorderSize("<<fBorderSize<<");"<<std::endl;
207  }
208  SaveFillAttributes(out,"pl",19,1001);
209  SaveLineAttributes(out,"pl",1,1,1);
210  SaveTextAttributes(out,"pl",22,0,1,62,0);
211 
212  out<<" pl->Draw();"<<std::endl;
213 }