Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TPaveText.cxx
Go to the documentation of this file.
1 // @(#)root/graf:$Id$
2 // Author: Rene Brun 20/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 <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 
16 #include "Riostream.h"
17 #include "TBufferFile.h"
18 #include "TROOT.h"
19 #include "TStyle.h"
20 #include "TPaveText.h"
21 #include "TPaveLabel.h"
22 #include "TVirtualPad.h"
23 #include "TMath.h"
24 #include "TLatex.h"
25 #include "TError.h"
26 #include "TColor.h"
27 #include "TClass.h"
28 
29 ClassImp(TPaveText);
30 
31 
32 /** \class TPaveText
33 \ingroup BasicGraphics
34 
35 A Pave (see TPave) with text, lines or/and boxes inside.
36 
37 Line (and boxes) are positioned in the pave using coordinates relative to
38 the pave (%).
39 
40 The text lines are added in order using the AddText method. Also line separators
41 can be added, in order too, using the AddLine method.
42 
43 AddText returns a TText corresponding to the line added to the pave. This
44 return value can be used to modify the text attributes.
45 
46 Once the TPaveText is build the text of each line can be retrieved using
47 GetLine or GetLineWith as a TText wich is useful to modify the text attributes
48 of a line.
49 
50 Example:
51 Begin_Macro(source)
52 ../../../tutorials/graphics/pavetext.C
53 End_Macro
54 
55 GetListOfLines can also be used to retrieve all the lines in the TPaveText as
56 a TList:
57 
58 Begin_Macro(source)
59 {
60  TPaveText *t = new TPaveText(.05,.3,.95,.6);
61  t->AddText("This line is blue"); ((TText*)t->GetListOfLines()->Last())->SetTextColor(kBlue);
62  t->AddText("This line is red"); ((TText*)t->GetListOfLines()->Last())->SetTextColor(kRed);
63  t->Draw();
64 }
65 End_Macro
66 
67 */
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// pavetext default constructor.
71 
72 TPaveText::TPaveText(): TPave(), TAttText()
73 {
74  fLines = 0;
75  fMargin = 0.05;
76  fLongest = 0;
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// PaveText normal constructor.
81 ///
82 /// A PaveText is a Pave with several lines of text
83 ///
84 /// - option = "TR" Top and Right shadows are drawn.
85 /// - option = "TL" Top and Left shadows are drawn.
86 /// - option = "BR" Bottom and Right shadows are drawn.
87 /// - option = "BL" Bottom and Left shadows are drawn.
88 ///
89 /// If none of these four above options is specified the default the
90 /// option "BR" will be used to draw the border. To produces a pave
91 /// without any border it is enough to specify the option "NB" (no border).
92 ///
93 /// - option = "NDC" x1,y1,x2,y2 are given in NDC
94 /// - option = "ARC" corners are rounded
95 ///
96 /// In case of option "ARC", the corner radius is specified
97 /// via TPave::SetCornerRadius(rad) where rad is given in percent
98 /// of the pave height (default value is 0.2).
99 ///
100 /// The individual text items are entered via AddText
101 /// By default, text items inherits from the default pavetext AttText.
102 /// A title can be added later to this pavetext via TPaveText::SetLabel.
103 
104 TPaveText::TPaveText(Double_t x1, Double_t y1,Double_t x2, Double_t y2, Option_t *option)
105  :TPave(x1,y1,x2,y2,4,option), TAttText(22,0,gStyle->GetTextColor(),gStyle->GetTextFont(),0)
106 {
107  fLines = new TList;
108  fMargin = 0.05;
109  fLongest = 0;
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// pavetext default destructor.
114 
115 TPaveText::~TPaveText()
116 {
117  if (!TestBit(kNotDeleted)) return;
118  if (fLines) fLines->Delete();
119  delete fLines;
120  fLines = 0;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// pavetext copy constructor.
125 
126 TPaveText::TPaveText(const TPaveText &pavetext) : TPave(), TAttText()
127 {
128  TBufferFile b(TBuffer::kWrite);
129  TPaveText *p = (TPaveText*)(&pavetext);
130  p->Streamer(b);
131  b.SetReadMode();
132  b.SetBufferOffset(0);
133  fLines = 0;
134  Streamer(b);
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 ///assignment operator
139 
140 TPaveText& TPaveText::operator=(const TPaveText& pt)
141 {
142  if(this!=&pt) {
143  TPave::operator=(pt);
144  TAttText::operator=(pt);
145  fLabel=pt.fLabel;
146  fLongest=pt.fLongest;
147  fMargin=pt.fMargin;
148  fLines=pt.fLines;
149  }
150  return *this;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Add a new graphics box to this pavetext.
155 
156 TBox *TPaveText::AddBox(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
157 {
158  if (!gPad->IsEditable()) return 0;
159  TBox *newbox = new TBox(x1,y1,x2,y2);
160 
161  if (!fLines) fLines = new TList;
162  fLines->Add(newbox);
163  return newbox;
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Add a new graphics line to this pavetext.
168 
169 TLine *TPaveText::AddLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
170 {
171  if (!gPad->IsEditable()) return 0;
172  TLine *newline = new TLine(x1,y1,x2,y2);
173 
174  if (!fLines) fLines = new TList;
175  fLines->Add(newline);
176  return newline;
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Add a new Text line to this pavetext at given coordinates.
181 
182 TText *TPaveText::AddText(Double_t x1, Double_t y1, const char *text)
183 {
184  TLatex *newtext = new TLatex(x1,y1,text);
185  newtext->SetTextAlign(0);
186  newtext->SetTextColor(0);
187  newtext->SetTextFont(0);
188  newtext->SetTextSize(0);
189  Int_t nch = strlen(text);
190  if (nch > fLongest) fLongest = nch;
191 
192  if (!fLines) fLines = new TList;
193  fLines->Add(newtext);
194  return newtext;
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// Add a new Text line to this pavetext.
199 
200 TText *TPaveText::AddText(const char *text)
201 {
202  return AddText(0,0,text);
203 }
204 
205 ////////////////////////////////////////////////////////////////////////////////
206 /// Clear all lines in this pavetext.
207 
208 void TPaveText::Clear(Option_t *)
209 {
210  if (!fLines) return;
211  fLines->Delete();
212  fLongest = 0;
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Delete text at the mouse position.
217 
218 void TPaveText::DeleteText()
219 {
220  if (!gPad->IsEditable()) return;
221  if (!fLines) return;
222  Double_t ymouse, yobj;
223  TObject *obj = GetObject(ymouse, yobj); //get object pointed by the mouse
224  if (!obj) return;
225  if (!obj->InheritsFrom(TText::Class())) return;
226  fLines->Remove(obj);
227  delete obj;
228 }
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// Draw this pavetext with its current attributes.
232 
233 void TPaveText::Draw(Option_t *option)
234 {
235  Option_t *opt;
236  if (option && strlen(option)) opt = option;
237  else opt = GetOption();
238 
239  AppendPad(opt);
240 }
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// Draw lines in filename in this pavetext.
244 
245 void TPaveText::DrawFile(const char *filename, Option_t *option)
246 {
247  ReadFile(filename);
248 
249  AppendPad(option);
250 }
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// Edit text at the mouse position.
254 
255 void TPaveText::EditText()
256 {
257  if (!gPad->IsEditable()) return;
258  Double_t ymouse, yobj;
259  TObject *obj = GetObject(ymouse, yobj); //get object pointed by the mouse
260  if (!obj) return;
261  if (!obj->InheritsFrom(TText::Class())) return;
262  TText *text = (TText*)obj;
263  gROOT->SetSelectedPrimitive(text);
264  gROOT->ProcessLine(Form("((TCanvas*)0x%lx)->SetSelected((TObject*)0x%lx)",
265  (ULong_t)gPad->GetCanvas(), (ULong_t)text));
266  gROOT->ProcessLine(Form("((TCanvas*)0x%lx)->Selected((TVirtualPad*)0x%lx,(TObject*)0x%lx,1)",
267  (ULong_t)gPad->GetCanvas(), (ULong_t)gPad, (ULong_t)text));
268  text->SetTextAttributes();
269 }
270 
271 ////////////////////////////////////////////////////////////////////////////////
272 /// Get Pointer to line number in this pavetext.
273 
274 TText *TPaveText::GetLine(Int_t number) const
275 {
276  TText *line;
277  TIter next(fLines);
278  Int_t nlines = 0;
279  while ((line = (TText*) next())) {
280  if (nlines == number) return line;
281  nlines++;
282  }
283  return 0;
284 }
285 
286 ////////////////////////////////////////////////////////////////////////////////
287 /// Get Pointer to first containing string text in this pavetext.
288 
289 TText *TPaveText::GetLineWith(const char *text) const
290 {
291  TText *line;
292  TIter next(fLines);
293  while ((line = (TText*) next())) {
294  if (strstr(line->GetTitle(),text)) return line;
295  }
296  return 0;
297 }
298 
299 ////////////////////////////////////////////////////////////////////////////////
300 /// Get object pointed by the mouse in this pavetext.
301 
302 TObject *TPaveText::GetObject(Double_t &ymouse, Double_t &yobj) const
303 {
304  if (!fLines) return 0;
305  Int_t nlines = GetSize();
306  if (nlines == 0) return 0;
307 
308  // Evaluate text size as a function of the number of lines
309 
310  ymouse = gPad->AbsPixeltoY(gPad->GetEventY());
311  Double_t yspace = (fY2 - fY1)/Double_t(nlines);
312  Double_t y1,y,dy;
313  Double_t ytext = fY2 + 0.5*yspace;
314  Int_t valign;
315 
316  // Iterate over all lines
317  // Copy pavetext attributes to line attributes if line attributes not set
318  dy = fY2 - fY1;
319  TObject *line;
320  TText *linet;
321  TLine *linel;
322  TBox *lineb;
323  TIter next(fLines);
324  while ((line = (TObject*) next())) {
325  // Next primitive is a line
326  if (line->IsA() == TLine::Class()) {
327  linel = (TLine*)line;
328  y1 = linel->GetY1(); if (y1 == 0) y1 = ytext; else y1 = fY1 + y1*dy;
329  if (TMath::Abs(y1-ymouse) < 0.2*yspace) {yobj = y1; return line;}
330  continue;
331  }
332  // Next primitive is a box
333  if (line->IsA() == TBox::Class()) {
334  lineb = (TBox*)line;
335  y1 = lineb->GetY1(); if (y1 == 0) y1 = ytext; else y1 = fY1 + y1*dy;
336  if (TMath::Abs(y1-ymouse) < 0.4*yspace) {yobj = y1; return line;}
337  continue;
338  }
339  // Next primitive is a text
340  if (line->InheritsFrom(TText::Class())) {
341  linet = (TText*)line;
342  ytext -= yspace;
343  Double_t yl = linet->GetY();
344  Short_t talign = linet->GetTextAlign();
345  if (talign == 0) talign = GetTextAlign();
346  if (yl > 0 && yl <1) {
347  ytext = fY1 + yl*dy;
348  }
349  valign = linet->GetTextAlign()%10;
350  y = ytext;
351  if (valign == 1) y = ytext -0.5*yspace;
352  if (valign == 3) y = ytext +0.5*yspace;
353 
354  if (TMath::Abs(y-ymouse) < 0.5*yspace) {yobj = y; return line;}
355  }
356  }
357  return 0;
358 }
359 
360 ////////////////////////////////////////////////////////////////////////////////
361 /// return number of text lines (ignoring TLine, etc)
362 
363 Int_t TPaveText::GetSize() const
364 {
365  Int_t nlines = 0;
366  TIter next(fLines);
367  TObject *line;
368  while ((line = (TObject*) next())) {
369  if (line->InheritsFrom(TText::Class())) nlines++;
370  }
371  return nlines;
372 }
373 
374 ////////////////////////////////////////////////////////////////////////////////
375 /// Add a new line at the mouse position.
376 
377 void TPaveText::InsertLine()
378 {
379  if (!gPad->IsEditable()) return;
380  Double_t ymouse=0, yobj;
381  TObject *obj = GetObject(ymouse, yobj); //get object pointed by the mouse
382  Double_t yline = (ymouse-fY1)/(fY2-fY1);
383  TLine *newline = AddLine(0,yline,0,yline);
384  if (obj) {
385  fLines->Remove(newline); //remove line from last position
386  if (yobj < ymouse) fLines->AddBefore(obj,newline);
387  else fLines->AddAfter(obj,newline);
388  }
389 }
390 
391 ////////////////////////////////////////////////////////////////////////////////
392 /// Add a new Text line at the mouse position.
393 
394 void TPaveText::InsertText(const char *text)
395 {
396  if (!gPad->IsEditable()) return;
397  Double_t ymouse, yobj;
398  TObject *obj = GetObject(ymouse, yobj); //get object pointed by the mouse
399  TText *newtext = AddText(0,0,text); //create new text object
400  if (obj) {
401  fLines->Remove(newtext); //remove text from last position
402  if (yobj < ymouse) fLines->AddBefore(obj,newtext); //insert new text at right position
403  else fLines->AddAfter(obj,newtext); //insert new text at right position
404  }
405 }
406 
407 ////////////////////////////////////////////////////////////////////////////////
408 /// Paint this pavetext with its current attributes.
409 
410 void TPaveText::Paint(Option_t *option)
411 {
412  // Draw the pave
413  TPave::ConvertNDCtoPad();
414  TPave::PaintPave(fX1,fY1,fX2,fY2,GetBorderSize(),option);
415  PaintPrimitives(kPaveText);
416 }
417 
418 ////////////////////////////////////////////////////////////////////////////////
419 /// Paint list of primitives in this pavetext.
420 
421 void TPaveText::PaintPrimitives(Int_t mode)
422 {
423  if (!fLines) return;
424  Double_t dx = fX2 - fX1;
425  Double_t dy = fY2 - fY1;
426  Double_t textsize = GetTextSize();
427  Int_t nlines = GetSize();
428  if (nlines == 0) nlines = 5;
429 
430  // Evaluate text size as a function of the number of lines
431 
432  Double_t x1,y1,x2,y2;
433  y1 = gPad->GetY1();
434  y2 = gPad->GetY2();
435  Float_t margin = fMargin*dx;
436  Double_t yspace = dy/Double_t(nlines);
437  Double_t textsave = textsize;
438  TObject *line;
439  TText *linet;
440  TLatex *latex;
441  TIter next(fLines);
442  Double_t longest = 0;
443  Double_t w;
444  if (textsize == 0) {
445  textsize = 0.85*yspace/(y2 - y1);
446  while ((line = (TObject*) next())) {
447  if (line->IsA() == TLatex::Class()) {
448  latex = (TLatex*)line;
449  Float_t tangle = latex->GetTextAngle();
450  if (latex->GetTextSize() != 0) continue;
451  Style_t tfont = latex->GetTextFont();
452  if (tfont == 0) latex->SetTextFont(GetTextFont());
453  latex->SetTextSize(textsize);
454  w = latex->GetXsize();
455  latex->SetTextSize(0);
456  latex->SetTextAngle(tangle); //text angle was redefined in GetXsize !
457  if (w > longest) longest = w;
458  latex->SetTextFont(tfont);
459  }
460  }
461  if (longest > 0.92*dx) textsize *= 0.92*dx/longest;
462  if (mode == kDiamond) textsize *= 0.66;
463  SetTextSize(textsize);
464  }
465  Double_t ytext = fY2 + 0.5*yspace;
466  Double_t xtext = 0;
467  Int_t halign;
468 
469  // Iterate over all lines
470  // Copy pavetext attributes to line attributes if line attributes not set
471  TLine *linel;
472  TBox *lineb;
473  next.Reset();
474  while ((line = (TObject*) next())) {
475  // Next primitive is a line
476  if (line->IsA() == TLine::Class()) {
477  linel = (TLine*)line;
478  x1 = linel->GetX1(); if (x1 == 0) x1 = fX1; else x1 = fX1 + x1*dx;
479  x2 = linel->GetX2(); if (x2 == 0) x2 = fX2; else x2 = fX1 + x2*dx;
480  y1 = linel->GetY1(); if (y1 == 0) y1 = ytext; else y1 = fY1 + y1*dy;
481  y2 = linel->GetY2(); if (y2 == 0) y2 = ytext; else y2 = fY1 + y2*dy;
482  linel->PaintLine(x1,y1,x2,y2);
483  continue;
484  }
485  // Next primitive is a box
486  if (line->IsA() == TBox::Class()) {
487  lineb = (TBox*)line;
488  x1 = lineb->GetX1();
489  if (x1) x1 = fX1 + x1*dx;
490  else x1 = fX1 + gPad->PixeltoX(1) - gPad->PixeltoX(0);
491  x2 = lineb->GetX2();
492  if (x2) x2 = fX1 + x2*dx;
493  else x2 = fX2;
494  y1 = lineb->GetY1(); if (y1 == 0) y1 = ytext; else y1 = fY1 + y1*dy;
495  y2 = lineb->GetY2(); if (y2 == 0) y2 = ytext; else y2 = fY1 + y2*dy;
496  lineb->PaintBox(x1,y1,x2,y2);
497  continue;
498  }
499  // Next primitive is a text
500  if (line->IsA() == TText::Class()) {
501  linet = (TText*)line;
502  ytext -= yspace;
503  Double_t xl = linet->GetX();
504  Double_t yl = linet->GetY();
505  Short_t talign = linet->GetTextAlign();
506  Color_t tcolor = linet->GetTextColor();
507  Style_t tfont = linet->GetTextFont();
508  Size_t tsize = linet->GetTextSize();
509  if (talign == 0) linet->SetTextAlign(GetTextAlign());
510  if (tcolor == 0) linet->SetTextColor(GetTextColor());
511  if (tfont == 0) linet->SetTextFont(GetTextFont());
512  if (tsize == 0) linet->SetTextSize(GetTextSize());
513  if (xl > 0 && xl <1) {
514  xtext = fX1 + xl*dx;
515  } else {
516  halign = linet->GetTextAlign()/10;
517  if (halign == 1) xtext = fX1 + margin;
518  if (halign == 2) xtext = 0.5*(fX1+fX2);
519  if (halign == 3) xtext = fX2 - margin;
520  }
521  if (yl > 0 && yl <1) ytext = fY1 + yl*dy;
522  linet->PaintText(xtext,ytext,linet->GetTitle());
523  linet->SetTextAlign(talign);
524  linet->SetTextColor(tcolor);
525  linet->SetTextFont(tfont);
526  linet->SetTextSize(tsize);
527  }
528  // Next primitive is a Latex text
529  if (line->IsA() == TLatex::Class()) {
530  latex = (TLatex*)line;
531  ytext -= yspace;
532  Double_t xl = latex->GetX();
533  Double_t yl = latex->GetY();
534  Short_t talign = latex->GetTextAlign();
535  Color_t tcolor = latex->GetTextColor();
536  Style_t tfont = latex->GetTextFont();
537  Size_t tsize = latex->GetTextSize();
538  if (talign == 0) latex->SetTextAlign(GetTextAlign());
539  if (tcolor == 0) latex->SetTextColor(GetTextColor());
540  if (tfont == 0) latex->SetTextFont(GetTextFont());
541  if (tsize == 0) latex->SetTextSize(GetTextSize());
542  if (xl > 0 && xl <1) {
543  xtext = fX1 + xl*dx;
544  } else {
545  halign = latex->GetTextAlign()/10;
546  if (halign == 1) xtext = fX1 + margin;
547  if (halign == 2) xtext = 0.5*(fX1+fX2);
548  if (halign == 3) xtext = fX2 - margin;
549  }
550  if (yl > 0 && yl <1) ytext = fY1 + yl*dy;
551  latex->PaintLatex(xtext,ytext,latex->GetTextAngle(),
552  latex->GetTextSize(),
553  latex->GetTitle());
554  latex->SetTextAlign(talign);
555  latex->SetTextColor(tcolor);
556  latex->SetTextFont(tfont);
557  latex->SetTextSize(tsize);
558  latex->SetX(xl); // PaintLatex modifies fX and fY
559  latex->SetY(yl);
560  }
561  }
562 
563  SetTextSize(textsave);
564 
565  // if a label create & paint a pavetext title
566  if (fLabel.Length() > 0) {
567  dy = gPad->GetY2() - gPad->GetY1();
568  x1 = fX1 + 0.25*dx;
569  x2 = fX2 - 0.25*dx;
570  y1 = fY2 - 0.02*dy;
571  y2 = fY2 + 0.02*dy;
572  TPaveLabel *title = new TPaveLabel(x1,y1,x2,y2,fLabel.Data(),GetDrawOption());
573  title->SetFillColor(GetFillColor());
574  title->SetTextColor(GetTextColor());
575  title->SetTextFont(GetTextFont());
576  title->Paint();
577  delete title;
578  }
579 }
580 
581 ////////////////////////////////////////////////////////////////////////////////
582 /// Dump this pavetext with its attributes.
583 
584 void TPaveText::Print(Option_t *option) const
585 {
586  TPave::Print(option);
587  if (fLines) fLines->Print();
588 }
589 
590 ////////////////////////////////////////////////////////////////////////////////
591 /// Read lines of filename in this pavetext.
592 ///
593 /// Read from line number fromline a total of nlines
594 ///
595 /// Note that this function changes the default text alignment to left/center
596 
597 void TPaveText::ReadFile(const char *filename, Option_t *option, Int_t nlines, Int_t fromline)
598 {
599  Int_t ival;
600  Float_t val;
601  TText *lastline = 0;
602  TString opt = option;
603  if (!opt.Contains("+")) {
604  Clear();
605  fLongest = 0;
606  }
607  SetTextAlign(12);
608  // Get file name
609  Int_t nch = strlen(filename);
610  if (nch == 0) return;
611 
612  char *fname = StrDup(filename);
613  if (fname[nch-1] == ';') { nch--; fname[nch]=0;}
614 
615  std::ifstream file(fname,std::ios::in);
616  if (!file.good()) {
617  Error("ReadFile", "illegal file name");
618  delete [] fname;
619  return;
620  }
621 
622  const int linesize = 255;
623  char currentline[linesize];
624  char *ss, *sclose, *s= 0;
625 
626  Int_t kline = 0;
627  while (1) {
628  file.getline(currentline,linesize);
629  if (file.eof())break;
630  if (kline >= fromline && kline < fromline+nlines) {
631  s = currentline;
632  if (strstr(s,"+SetText")) {
633  ss = s+8;
634  sclose = strstr(ss,")");
635  if (!sclose) continue;
636  *sclose = 0;
637  lastline = (TText*)fLines->Last();
638  if (!lastline) continue;
639  if (strstr(ss,"Color(")) {
640  sscanf(ss+6,"%d",&ival);
641  lastline->SetTextColor(ival);
642  continue;
643  }
644  if (strstr(ss,"Align(")) {
645  sscanf(ss+6,"%d",&ival);
646  lastline->SetTextAlign(ival);
647  continue;
648  }
649  if (strstr(ss,"Font(")) {
650  sscanf(ss+5,"%d",&ival);
651  lastline->SetTextFont(ival);
652  continue;
653  }
654  if (strstr(ss,"Size(")) {
655  sscanf(ss+5,"%f",&val);
656  lastline->SetTextSize(val);
657  continue;
658  }
659  if (strstr(ss,"Angle(")) {
660  sscanf(ss+6,"%f",&val);
661  lastline->SetTextAngle(val);
662  continue;
663  }
664  }
665  AddText(s);
666  }
667  kline++;
668  }
669  file.close();
670  delete [] fname;
671 }
672 
673 ////////////////////////////////////////////////////////////////////////////////
674 /// Save lines of this pavetext as C++ statements on output stream out
675 
676 void TPaveText::SaveLines(std::ostream &out, const char *name, Bool_t saved)
677 {
678  if (!fLines) return;
679  Int_t nlines = GetSize();
680  if (nlines == 0) return;
681 
682  // Iterate over all lines
683  char quote = '"';
684  TObject *line;
685  TText *linet;
686  TLatex *latex;
687  TLine *linel;
688  TBox *lineb;
689  TIter next(fLines);
690  Bool_t savedlt = kFALSE;
691  Bool_t savedt = kFALSE;
692  Bool_t savedl = kFALSE;
693  Bool_t savedb = kFALSE;
694 
695  while ((line = (TObject*) next())) {
696  // Next primitive is a line
697  if (line->IsA() == TLine::Class()) {
698  linel = (TLine*)line;
699  if (saved || savedl) {
700  out<<" ";
701  } else {
702  out<<" TLine *";
703  savedl = kTRUE;
704  }
705  out<<name<<"_Line = "<<name<<"->AddLine("
706  <<linel->GetX1()<<","<<linel->GetY1()<<","<<linel->GetX2()<<","<<linel->GetY2()<<");"<<std::endl;
707  if (linel->GetLineColor() != 1) {
708  if (linel->GetLineColor() > 228) {
709  TColor::SaveColor(out, linel->GetLineColor());
710  out<<" "<<name<<"_Line->SetLineColor(ci);" << std::endl;
711  } else
712  out<<" "<<name<<"_Line->SetLineColor("<<linel->GetLineColor()<<");"<<std::endl;
713  }
714  if (linel->GetLineStyle() != 1) {
715  out<<" "<<name<<"_Line->SetLineStyle("<<linel->GetLineStyle()<<");"<<std::endl;
716  }
717  if (linel->GetLineWidth() != 1) {
718  out<<" "<<name<<"_Line->SetLineWidth("<<linel->GetLineWidth()<<");"<<std::endl;
719  }
720  continue;
721  }
722  // Next primitive is a box
723  if (line->IsA() == TBox::Class()) {
724  lineb = (TBox*)line;
725  if (saved || savedb) {
726  out<<" ";
727  } else {
728  out<<" TBox *";
729  savedb = kTRUE;
730  }
731  out<<name<<"_Box = "<<name<<"->AddBox("
732  <<lineb->GetX1()<<","<<lineb->GetY1()<<","<<lineb->GetX2()<<","<<lineb->GetY2()<<");"<<std::endl;
733  if (lineb->GetFillColor() != 18) {
734  if (lineb->GetFillColor() > 228) {
735  TColor::SaveColor(out, lineb->GetFillColor());
736  out<<" "<<name<<"_Box->SetFillColor(ci);" << std::endl;
737  } else
738  out<<" "<<name<<"_Box->SetFillColor("<<lineb->GetFillColor()<<");"<<std::endl;
739  }
740  if (lineb->GetFillStyle() != 1001) {
741  out<<" "<<name<<"_Box->SetFillStyle("<<lineb->GetFillStyle()<<");"<<std::endl;
742  }
743  if (lineb->GetLineColor() != 1) {
744  if (lineb->GetLineColor() > 228) {
745  TColor::SaveColor(out, lineb->GetLineColor());
746  out<<" "<<name<<"_Box->SetLineColor(ci);" << std::endl;
747  } else
748  out<<" "<<name<<"_Box->SetLineColor("<<lineb->GetLineColor()<<");"<<std::endl;
749  }
750  if (lineb->GetLineStyle() != 1) {
751  out<<" "<<name<<"_Box->SetLineStyle("<<lineb->GetLineStyle()<<");"<<std::endl;
752  }
753  if (lineb->GetLineWidth() != 1) {
754  out<<" "<<name<<"_Box->SetLineWidth("<<lineb->GetLineWidth()<<");"<<std::endl;
755  }
756  continue;
757  }
758  // Next primitive is a text
759  if (line->IsA() == TText::Class()) {
760  linet = (TText*)line;
761  if (saved || savedt) {
762  out<<" ";
763  } else {
764  out<<" TText *";
765  savedt = kTRUE;
766  }
767  if (!linet->GetX() && !linet->GetY()) {
768  TString s = linet->GetTitle();
769  s.ReplaceAll("\"","\\\"");
770  out<<name<<"_Text = "<<name<<"->AddText("
771  <<quote<<s.Data()<<quote<<");"<<std::endl;
772  } else {
773  out<<name<<"_Text = "<<name<<"->AddText("
774  <<linet->GetX()<<","<<linet->GetY()<<","<<quote<<linet->GetTitle()<<quote<<");"<<std::endl;
775  }
776  if (linet->GetTextColor()) {
777  if (linet->GetTextColor() > 228) {
778  TColor::SaveColor(out, linet->GetTextColor());
779  out<<" "<<name<<"_Text->SetTextColor(ci);" << std::endl;
780  } else
781  out<<" "<<name<<"_Text->SetTextColor("<<linet->GetTextColor()<<");"<<std::endl;
782  }
783  if (linet->GetTextFont()) {
784  out<<" "<<name<<"_Text->SetTextFont("<<linet->GetTextFont()<<");"<<std::endl;
785  }
786  if (linet->GetTextSize()) {
787  out<<" "<<name<<"_Text->SetTextSize("<<linet->GetTextSize()<<");"<<std::endl;
788  }
789  if (linet->GetTextAngle() != GetTextAngle()) {
790  out<<" "<<name<<"_Text->SetTextAngle("<<linet->GetTextAngle()<<");"<<std::endl;
791  }
792  if (linet->GetTextAlign()) {
793  out<<" "<<name<<"_Text->SetTextAlign("<<linet->GetTextAlign()<<");"<<std::endl;
794  }
795  }
796  // Next primitive is a Latex text
797  if (line->IsA() == TLatex::Class()) {
798  latex = (TLatex*)line;
799  if (saved || savedlt) {
800  out<<" ";
801  } else {
802  out<<" TText *";
803  savedlt = kTRUE;
804  }
805  if (!latex->GetX() && !latex->GetY()) {
806  TString sl = latex->GetTitle();
807  sl.ReplaceAll("\"","\\\"");
808  out<<name<<"_LaTex = "<<name<<"->AddText("
809  <<quote<<sl.Data()<<quote<<");"<<std::endl;
810  } else {
811  out<<name<<"_LaTex = "<<name<<"->AddText("
812  <<latex->GetX()<<","<<latex->GetY()<<","<<quote<<latex->GetTitle()<<quote<<");"<<std::endl;
813  }
814  if (latex->GetTextColor()) {
815  if (latex->GetTextColor() > 228) {
816  TColor::SaveColor(out, latex->GetTextColor());
817  out<<" "<<name<<"_LaTex->SetTextColor(ci);" << std::endl;
818  } else
819  out<<" "<<name<<"_LaTex->SetTextColor("<<latex->GetTextColor()<<");"<<std::endl;
820  }
821  if (latex->GetTextFont()) {
822  out<<" "<<name<<"_LaTex->SetTextFont("<<latex->GetTextFont()<<");"<<std::endl;
823  }
824  if (latex->GetTextSize()) {
825  out<<" "<<name<<"_LaTex->SetTextSize("<<latex->GetTextSize()<<");"<<std::endl;
826  }
827  if (latex->GetTextAngle() != GetTextAngle()) {
828  out<<" "<<name<<"_LaTex->SetTextAngle("<<latex->GetTextAngle()<<");"<<std::endl;
829  }
830  if (latex->GetTextAlign()) {
831  out<<" "<<name<<"_LaTex->SetTextAlign("<<latex->GetTextAlign()<<");"<<std::endl;
832  }
833  }
834  }
835 }
836 
837 ////////////////////////////////////////////////////////////////////////////////
838 /// Save primitive as a C++ statement(s) on output stream out
839 
840 void TPaveText::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
841 {
842  char quote = '"';
843  Bool_t saved = gROOT->ClassSaved(TPaveText::Class());
844  out<<" "<<std::endl;
845  if (saved) {
846  out<<" ";
847  } else {
848  out<<" "<<ClassName()<<" *";
849  }
850  if (fOption.Contains("NDC")) {
851  out<<"pt = new "<<ClassName()<<"("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
852  <<","<<quote<<fOption<<quote<<");"<<std::endl;
853  } else {
854  out<<"pt = new "<<ClassName()<<"("<<gPad->PadtoX(fX1)<<","<<gPad->PadtoY(fY1)<<","<<gPad->PadtoX(fX2)<<","<<gPad->PadtoY(fY2)
855  <<","<<quote<<fOption<<quote<<");"<<std::endl;
856  }
857  if (strcmp(GetName(),"TPave")) {
858  out<<" pt->SetName("<<quote<<GetName()<<quote<<");"<<std::endl;
859  }
860  if (fLabel.Length() > 0) {
861  out<<" pt->SetLabel("<<quote<<fLabel<<quote<<");"<<std::endl;
862  }
863  if (fBorderSize != 4) {
864  out<<" pt->SetBorderSize("<<fBorderSize<<");"<<std::endl;
865  }
866  SaveFillAttributes(out,"pt",19,1001);
867  SaveLineAttributes(out,"pt",1,1,1);
868  SaveTextAttributes(out,"pt",22,0,1,62,0);
869  SaveLines(out,"pt",saved);
870  out<<" pt->Draw();"<<std::endl;
871 }
872 
873 ////////////////////////////////////////////////////////////////////////////////
874 /// Set attribute option for all lines containing string text.
875 ///
876 /// Possible options are all the AttText attributes
877 /// Align, Color, Font, Size and Angle
878 
879 void TPaveText::SetAllWith(const char *text, Option_t *option, Double_t value)
880 {
881  TString opt=option;
882  opt.ToLower();
883  TText *line;
884  TIter next(fLines);
885  while ((line = (TText*) next())) {
886  if (strstr(line->GetTitle(),text)) {
887  if (opt == "align") line->SetTextAlign(Int_t(value));
888  if (opt == "color") line->SetTextColor(Int_t(value));
889  if (opt == "font") line->SetTextFont(Int_t(value));
890  if (opt == "size") line->SetTextSize(value);
891  if (opt == "angle") line->SetTextAngle(value);
892  }
893  }
894 }
895 
896 ////////////////////////////////////////////////////////////////////////////////
897 /// Stream an object of class TPaveText.
898 
899 void TPaveText::Streamer(TBuffer &R__b)
900 {
901  if (R__b.IsReading()) {
902  UInt_t R__s, R__c;
903  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
904  if (R__v > 1) {
905  R__b.ReadClassBuffer(TPaveText::Class(), this, R__v, R__s, R__c);
906  return;
907  }
908  //====process old versions before automatic schema evolution
909  TPave::Streamer(R__b);
910  TAttText::Streamer(R__b);
911  if (R__v > 1) fLabel.Streamer(R__b);
912  R__b >> fLongest;
913  R__b >> fMargin;
914  R__b >> fLines;
915  R__b.CheckByteCount(R__s, R__c, TPaveText::IsA());
916  //====end of old versions
917 
918  } else {
919  R__b.WriteClassBuffer(TPaveText::Class(),this);
920  }
921 }
922 
923 ////////////////////////////////////////////////////////////////////////////////
924 /// Replace current attributes by current style.
925 
926 void TPaveText::UseCurrentStyle()
927 {
928  if (gStyle->IsReading()) {
929  SetTextFont(gStyle->GetTextFont());
930  SetTextSize(gStyle->GetTextSize());
931  SetTextColor(gStyle->GetTextColor());
932  } else {
933  gStyle->SetTextColor(GetTextColor());
934  gStyle->SetTextFont(GetTextFont());
935  gStyle->SetTextSize(GetTextSize());
936  }
937 }