TPaveLabel.cxx

Go to the documentation of this file.
00001 // @(#)root/graf:$Id: TPaveLabel.cxx 32512 2010-03-09 20:50:38Z brun $
00002 // Author: Rene Brun   17/10/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "TROOT.h"
00014 #include "TStyle.h"
00015 #include "TPaveLabel.h"
00016 #include "TLatex.h"
00017 #include "TVirtualPad.h"
00018 
00019 ClassImp(TPaveLabel)
00020 
00021 
00022 //______________________________________________________________________________
00023 //*  A PaveLabel is a Pave (see TPave) with a text centered in the Pave.
00024 //Begin_Html
00025 /*
00026 <img src="gif/pavelabel.gif">
00027 */
00028 //End_Html
00029 //
00030 
00031 
00032 //______________________________________________________________________________
00033 TPaveLabel::TPaveLabel(): TPave(), TAttText()
00034 {
00035    // Pavelabel default constructor.
00036 }
00037 
00038 
00039 //______________________________________________________________________________
00040 TPaveLabel::TPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t  y2, const char *label, Option_t *option)
00041            :TPave(x1,y1,x2,y2,3,option), TAttText(22,0,1,gStyle->GetTextFont(),0.99)
00042 {
00043    // Pavelabel normal constructor.
00044    //
00045    // a PaveLabel is a Pave with a label centered in the Pave
00046    // The Pave is by default defined bith bordersize=5 and option ="br".
00047    // The text size is automatically computed as a function of the pave size.
00048    //
00049    //  IMPORTANT NOTE:
00050    //  Because TPave objects (and objects deriving from TPave) have their
00051    //  master coordinate system in NDC, one cannot use the TBox functions
00052    //  SetX1,SetY1,SetX2,SetY2 to change the corner coordinates. One should use
00053    //  instead SetX1NDC, SetY1NDC, SetX2NDC, SetY2NDC.
00054 
00055    fLabel  = label;
00056 }
00057 
00058 
00059 //______________________________________________________________________________
00060 TPaveLabel::~TPaveLabel()
00061 {
00062    // Pavelabel default destructor.
00063 }
00064 
00065 
00066 //______________________________________________________________________________
00067 TPaveLabel::TPaveLabel(const TPaveLabel &pavelabel) : TPave(pavelabel), TAttText(pavelabel)
00068 {
00069    // Pavelabel copy constructor.
00070 
00071    ((TPaveLabel&)pavelabel).Copy(*this);
00072 }
00073 
00074 
00075 //______________________________________________________________________________
00076 void TPaveLabel::Copy(TObject &obj) const
00077 {
00078    // Copy this pavelabel to pavelabel.
00079 
00080    TPave::Copy(obj);
00081    TAttText::Copy(((TPaveLabel&)obj));
00082    ((TPaveLabel&)obj).fLabel      = fLabel;
00083 }
00084 
00085 
00086 //______________________________________________________________________________
00087 void TPaveLabel::Draw(Option_t *option)
00088 {
00089    // Draw this pavelabel with its current attributes.
00090 
00091    Option_t *opt;
00092    if (option && strlen(option)) opt = option;
00093    else                          opt = GetOption();
00094 
00095    AppendPad(opt);
00096 }
00097 
00098 
00099 //______________________________________________________________________________
00100 void TPaveLabel::DrawPaveLabel(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char *label, Option_t *option)
00101 {
00102    // Draw this pavelabel with new coordinates.
00103 
00104    TPaveLabel *newpavelabel = new TPaveLabel(x1,y1,x2,y2,label,option);
00105    newpavelabel->SetBit(kCanDelete);
00106    newpavelabel->AppendPad();
00107 }
00108 
00109 
00110 //______________________________________________________________________________
00111 void TPaveLabel::Paint(Option_t *option)
00112 {
00113    // Paint this pavelabel with its current attributes.
00114 
00115    // Convert from NDC to pad coordinates
00116    TPave::ConvertNDCtoPad();
00117 
00118    PaintPaveLabel(fX1, fY1, fX2, fY2, GetLabel(), strlen(option)?option:GetOption());
00119 }
00120 
00121 
00122 //______________________________________________________________________________
00123 void TPaveLabel::PaintPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t  y2,
00124                       const char *label ,Option_t *option)
00125 {
00126    // Draw this pavelabel with new coordinates.
00127 
00128    Int_t nch = strlen(label);
00129 
00130    // Draw the pave
00131    TPave::PaintPave(x1,y1,x2,y2,GetBorderSize(),option);
00132 
00133    Float_t nspecials = 0;
00134    for (Int_t i=0;i<nch;i++) {
00135       if (label[i] == '!') nspecials += 1;
00136       if (label[i] == '?') nspecials += 1.5;
00137       if (label[i] == '#') nspecials += 1;
00138       if (label[i] == '`') nspecials += 1;
00139       if (label[i] == '^') nspecials += 1.5;
00140       if (label[i] == '~') nspecials += 1;
00141       if (label[i] == '&') nspecials += 2;
00142       if (label[i] == '\\') nspecials += 3;  // octal characters very likely
00143    }
00144    nch -= Int_t(nspecials + 0.5);
00145    if (nch <= 0) return;
00146 
00147    // Draw label
00148    Double_t wh   = (Double_t)gPad->XtoPixel(gPad->GetX2());
00149    Double_t hh   = (Double_t)gPad->YtoPixel(gPad->GetY1());
00150    Double_t labelsize, textsize = GetTextSize();
00151    Int_t automat = 0;
00152    if (GetTextFont()%10 > 2) {  // fixed size font specified in pixels
00153       labelsize = GetTextSize();
00154    } else {
00155       if (TMath::Abs(textsize -0.99) < 0.001) automat = 1;
00156       if (textsize == 0)   { textsize = 0.99; automat = 1;}
00157       Int_t ypixel      = TMath::Abs(gPad->YtoPixel(y1) - gPad->YtoPixel(y2));
00158       labelsize = textsize*ypixel/hh;
00159       if (wh < hh) labelsize *= hh/wh;
00160    }
00161    TLatex latex;
00162    latex.SetTextAngle(GetTextAngle());
00163    latex.SetTextFont(GetTextFont());
00164    latex.SetTextAlign(GetTextAlign());
00165    latex.SetTextColor(GetTextColor());
00166    latex.SetTextSize(labelsize);
00167    if (automat) {
00168       UInt_t w,h;
00169       latex.GetTextExtent(w,h,GetTitle());
00170       labelsize = h/hh;
00171       Double_t wxlabel   = TMath::Abs(gPad->XtoPixel(x2) - gPad->XtoPixel(x1));
00172       while (w > 0.99*wxlabel) { labelsize *= 0.99*wxlabel/w;latex.SetTextSize(labelsize); latex.GetTextExtent(w,h,GetTitle());}
00173       if (h < 1) h = 1;
00174       if (h==1) {
00175       labelsize   = Double_t(h)/hh;
00176       if (wh < hh) labelsize *= hh/wh;
00177       latex.SetTextSize(labelsize);
00178       }
00179    }
00180    Int_t halign = GetTextAlign()/10;
00181    Int_t valign = GetTextAlign()%10;
00182    Double_t x = 0.5*(x1+x2);
00183    if (halign == 1) x = x1 + 0.02*(x2-x1);
00184    if (halign == 3) x = x2 - 0.02*(x2-x1);
00185    Double_t y = 0.5*(y1+y2);
00186    if (valign == 1) y = y1 + 0.02*(y2-y1);
00187    if (valign == 3) y = y2 - 0.02*(y2-y1);
00188    latex.PaintLatex(x, y, GetTextAngle(),labelsize,GetLabel());
00189 }
00190 
00191 
00192 //______________________________________________________________________________
00193 void TPaveLabel::SavePrimitive(ostream &out, Option_t * /*= ""*/)
00194 {
00195    // Save primitive as a C++ statement(s) on output stream out
00196 
00197    char quote = '"';
00198    out<<"   "<<endl;
00199    if (gROOT->ClassSaved(TPaveLabel::Class())) {
00200       out<<"   ";
00201    } else {
00202       out<<"   TPaveLabel *";
00203    }
00204    TString s = fLabel.Data();
00205    s.ReplaceAll("\"","\\\"");
00206    if (fOption.Contains("NDC")) {
00207       out<<"pl = new TPaveLabel("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
00208          <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<endl;
00209    } else {
00210       out<<"pl = new TPaveLabel("<<gPad->PadtoX(fX1)<<","<<gPad->PadtoY(fY1)<<","<<gPad->PadtoX(fX2)<<","<<gPad->PadtoY(fY2)
00211          <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<endl;
00212    }
00213    if (fBorderSize != 3) {
00214       out<<"   pl->SetBorderSize("<<fBorderSize<<");"<<endl;
00215    }
00216    SaveFillAttributes(out,"pl",19,1001);
00217    SaveLineAttributes(out,"pl",1,1,1);
00218    SaveTextAttributes(out,"pl",22,0,1,62,0);
00219 
00220    out<<"   pl->Draw();"<<endl;
00221 }

Generated on Tue Jul 5 14:14:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1