// @(#)root/graf:$Id$
// Author: Rene Brun   17/10/95

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TStyle.h"
#include "TPaveLabel.h"
#include "TLatex.h"
#include "TVirtualPad.h"

ClassImp(TPaveLabel)


//______________________________________________________________________________
//*  A PaveLabel is a Pave (see TPave) with a text centered in the Pave.
//Begin_Html
/*
<img src="gif/pavelabel.gif">
*/
//End_Html
//


//______________________________________________________________________________
TPaveLabel::TPaveLabel(): TPave(), TAttText()
{
   // Pavelabel default constructor.
}


//______________________________________________________________________________
TPaveLabel::TPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t  y2, const char *label, Option_t *option)
           :TPave(x1,y1,x2,y2,3,option), TAttText(22,0,1,gStyle->GetTextFont(),0.99)
{
   // Pavelabel normal constructor.
   //
   // a PaveLabel is a Pave with a label centered in the Pave
   // The Pave is by default defined bith bordersize=5 and option ="br".
   // The text size is automatically computed as a function of the pave size.

   fLabel  = label;
}


//______________________________________________________________________________
TPaveLabel::~TPaveLabel()
{
   // Pavelabel default destructor.
}


//______________________________________________________________________________
TPaveLabel::TPaveLabel(const TPaveLabel &pavelabel) : TPave(pavelabel), TAttText(pavelabel)
{
   // Pavelabel copy constructor.

   ((TPaveLabel&)pavelabel).Copy(*this);
}


//______________________________________________________________________________
void TPaveLabel::Copy(TObject &obj) const
{
   // Copy this pavelabel to pavelabel.

   TPave::Copy(obj);
   TAttText::Copy(((TPaveLabel&)obj));
   ((TPaveLabel&)obj).fLabel      = fLabel;
}


//______________________________________________________________________________
void TPaveLabel::Draw(Option_t *option)
{
   // Draw this pavelabel with its current attributes.

   Option_t *opt;
   if (option && strlen(option)) opt = option;
   else                          opt = GetOption();

   AppendPad(opt);
}


//______________________________________________________________________________
void 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.

   TPaveLabel *newpavelabel = new TPaveLabel(x1,y1,x2,y2,label,option);
   newpavelabel->SetBit(kCanDelete);
   newpavelabel->AppendPad();
}


//______________________________________________________________________________
void TPaveLabel::Paint(Option_t *option)
{
   // Paint this pavelabel with its current attributes.

   // Convert from NDC to pad coordinates
   TPave::ConvertNDCtoPad();

   PaintPaveLabel(fX1, fY1, fX2, fY2, GetLabel(), strlen(option)?option:GetOption());
}


//______________________________________________________________________________
void TPaveLabel::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.

   Int_t nch = strlen(label);

   // Draw the pave
   TPave::PaintPave(x1,y1,x2,y2,GetBorderSize(),option);

   Float_t nspecials = 0;
   for (Int_t i=0;i<nch;i++) {
      if (label[i] == '!') nspecials += 1;
      if (label[i] == '?') nspecials += 1.5;
      if (label[i] == '#') nspecials += 1;
      if (label[i] == '`') nspecials += 1;
      if (label[i] == '^') nspecials += 1.5;
      if (label[i] == '~') nspecials += 1;
      if (label[i] == '&') nspecials += 2;
      if (label[i] == '\\') nspecials += 3;  // octal characters very likely
   }
   nch -= Int_t(nspecials + 0.5);
   if (nch <= 0) return;

   // Draw label
   Double_t wh   = (Double_t)gPad->XtoPixel(gPad->GetX2());
   Double_t hh   = (Double_t)gPad->YtoPixel(gPad->GetY1());
   Double_t labelsize, textsize = GetTextSize();
   Int_t automat = 0;
   if (GetTextFont()%10 > 2) {  // fixed size font specified in pixels
      labelsize = GetTextSize();
   } else {
      if (TMath::Abs(textsize -0.99) < 0.001) automat = 1;
      if (textsize == 0)   { textsize = 0.99; automat = 1;}
      Int_t ypixel      = TMath::Abs(gPad->YtoPixel(y1) - gPad->YtoPixel(y2));
      labelsize = textsize*ypixel/hh;
      if (wh < hh) labelsize *= hh/wh;
   }
   TLatex latex;
   latex.SetTextAngle(GetTextAngle());
   latex.SetTextFont(GetTextFont());
   latex.SetTextAlign(GetTextAlign());
   latex.SetTextColor(GetTextColor());
   latex.SetTextSize(labelsize);
   if (automat) {
      UInt_t w=0,h=0,w1=0;
      latex.GetTextExtent(w,h,GetTitle());
      if (!w) return;
      labelsize = h/hh;
      Double_t wxlabel   = TMath::Abs(gPad->XtoPixel(x2) - gPad->XtoPixel(x1));
      latex.GetTextExtent(w1,h,GetTitle());
      while (w > 0.99*wxlabel) {
         labelsize *= 0.99*wxlabel/w;
         latex.SetTextSize(labelsize);
         latex.GetTextExtent(w,h,GetTitle());
         if (w==w1) break;
         else w1=w;
      }
      if (h < 1) h = 1;
      if (h==1) {
      labelsize   = Double_t(h)/hh;
      if (wh < hh) labelsize *= hh/wh;
      latex.SetTextSize(labelsize);
      }
   }
   Int_t halign = GetTextAlign()/10;
   Int_t valign = GetTextAlign()%10;
   Double_t x = 0.5*(x1+x2);
   if (halign == 1) x = x1 + 0.02*(x2-x1);
   if (halign == 3) x = x2 - 0.02*(x2-x1);
   Double_t y = 0.5*(y1+y2);
   if (valign == 1) y = y1 + 0.02*(y2-y1);
   if (valign == 3) y = y2 - 0.02*(y2-y1);
   latex.PaintLatex(x, y, GetTextAngle(),labelsize,GetLabel());
}


//______________________________________________________________________________
void TPaveLabel::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
{
   // Save primitive as a C++ statement(s) on output stream out

   char quote = '"';
   out<<"   "<<std::endl;
   if (gROOT->ClassSaved(TPaveLabel::Class())) {
      out<<"   ";
   } else {
      out<<"   TPaveLabel *";
   }
   TString s = fLabel.Data();
   s.ReplaceAll("\"","\\\"");
   if (fOption.Contains("NDC")) {
      out<<"pl = new TPaveLabel("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
         <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
   } else {
      out<<"pl = new TPaveLabel("<<gPad->PadtoX(fX1)<<","<<gPad->PadtoY(fY1)<<","<<gPad->PadtoX(fX2)<<","<<gPad->PadtoY(fY2)
         <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
   }
   if (fBorderSize != 3) {
      out<<"   pl->SetBorderSize("<<fBorderSize<<");"<<std::endl;
   }
   SaveFillAttributes(out,"pl",19,1001);
   SaveLineAttributes(out,"pl",1,1,1);
   SaveTextAttributes(out,"pl",22,0,1,62,0);

   out<<"   pl->Draw();"<<std::endl;
}
 TPaveLabel.cxx:1
 TPaveLabel.cxx:2
 TPaveLabel.cxx:3
 TPaveLabel.cxx:4
 TPaveLabel.cxx:5
 TPaveLabel.cxx:6
 TPaveLabel.cxx:7
 TPaveLabel.cxx:8
 TPaveLabel.cxx:9
 TPaveLabel.cxx:10
 TPaveLabel.cxx:11
 TPaveLabel.cxx:12
 TPaveLabel.cxx:13
 TPaveLabel.cxx:14
 TPaveLabel.cxx:15
 TPaveLabel.cxx:16
 TPaveLabel.cxx:17
 TPaveLabel.cxx:18
 TPaveLabel.cxx:19
 TPaveLabel.cxx:20
 TPaveLabel.cxx:21
 TPaveLabel.cxx:22
 TPaveLabel.cxx:23
 TPaveLabel.cxx:24
 TPaveLabel.cxx:25
 TPaveLabel.cxx:26
 TPaveLabel.cxx:27
 TPaveLabel.cxx:28
 TPaveLabel.cxx:29
 TPaveLabel.cxx:30
 TPaveLabel.cxx:31
 TPaveLabel.cxx:32
 TPaveLabel.cxx:33
 TPaveLabel.cxx:34
 TPaveLabel.cxx:35
 TPaveLabel.cxx:36
 TPaveLabel.cxx:37
 TPaveLabel.cxx:38
 TPaveLabel.cxx:39
 TPaveLabel.cxx:40
 TPaveLabel.cxx:41
 TPaveLabel.cxx:42
 TPaveLabel.cxx:43
 TPaveLabel.cxx:44
 TPaveLabel.cxx:45
 TPaveLabel.cxx:46
 TPaveLabel.cxx:47
 TPaveLabel.cxx:48
 TPaveLabel.cxx:49
 TPaveLabel.cxx:50
 TPaveLabel.cxx:51
 TPaveLabel.cxx:52
 TPaveLabel.cxx:53
 TPaveLabel.cxx:54
 TPaveLabel.cxx:55
 TPaveLabel.cxx:56
 TPaveLabel.cxx:57
 TPaveLabel.cxx:58
 TPaveLabel.cxx:59
 TPaveLabel.cxx:60
 TPaveLabel.cxx:61
 TPaveLabel.cxx:62
 TPaveLabel.cxx:63
 TPaveLabel.cxx:64
 TPaveLabel.cxx:65
 TPaveLabel.cxx:66
 TPaveLabel.cxx:67
 TPaveLabel.cxx:68
 TPaveLabel.cxx:69
 TPaveLabel.cxx:70
 TPaveLabel.cxx:71
 TPaveLabel.cxx:72
 TPaveLabel.cxx:73
 TPaveLabel.cxx:74
 TPaveLabel.cxx:75
 TPaveLabel.cxx:76
 TPaveLabel.cxx:77
 TPaveLabel.cxx:78
 TPaveLabel.cxx:79
 TPaveLabel.cxx:80
 TPaveLabel.cxx:81
 TPaveLabel.cxx:82
 TPaveLabel.cxx:83
 TPaveLabel.cxx:84
 TPaveLabel.cxx:85
 TPaveLabel.cxx:86
 TPaveLabel.cxx:87
 TPaveLabel.cxx:88
 TPaveLabel.cxx:89
 TPaveLabel.cxx:90
 TPaveLabel.cxx:91
 TPaveLabel.cxx:92
 TPaveLabel.cxx:93
 TPaveLabel.cxx:94
 TPaveLabel.cxx:95
 TPaveLabel.cxx:96
 TPaveLabel.cxx:97
 TPaveLabel.cxx:98
 TPaveLabel.cxx:99
 TPaveLabel.cxx:100
 TPaveLabel.cxx:101
 TPaveLabel.cxx:102
 TPaveLabel.cxx:103
 TPaveLabel.cxx:104
 TPaveLabel.cxx:105
 TPaveLabel.cxx:106
 TPaveLabel.cxx:107
 TPaveLabel.cxx:108
 TPaveLabel.cxx:109
 TPaveLabel.cxx:110
 TPaveLabel.cxx:111
 TPaveLabel.cxx:112
 TPaveLabel.cxx:113
 TPaveLabel.cxx:114
 TPaveLabel.cxx:115
 TPaveLabel.cxx:116
 TPaveLabel.cxx:117
 TPaveLabel.cxx:118
 TPaveLabel.cxx:119
 TPaveLabel.cxx:120
 TPaveLabel.cxx:121
 TPaveLabel.cxx:122
 TPaveLabel.cxx:123
 TPaveLabel.cxx:124
 TPaveLabel.cxx:125
 TPaveLabel.cxx:126
 TPaveLabel.cxx:127
 TPaveLabel.cxx:128
 TPaveLabel.cxx:129
 TPaveLabel.cxx:130
 TPaveLabel.cxx:131
 TPaveLabel.cxx:132
 TPaveLabel.cxx:133
 TPaveLabel.cxx:134
 TPaveLabel.cxx:135
 TPaveLabel.cxx:136
 TPaveLabel.cxx:137
 TPaveLabel.cxx:138
 TPaveLabel.cxx:139
 TPaveLabel.cxx:140
 TPaveLabel.cxx:141
 TPaveLabel.cxx:142
 TPaveLabel.cxx:143
 TPaveLabel.cxx:144
 TPaveLabel.cxx:145
 TPaveLabel.cxx:146
 TPaveLabel.cxx:147
 TPaveLabel.cxx:148
 TPaveLabel.cxx:149
 TPaveLabel.cxx:150
 TPaveLabel.cxx:151
 TPaveLabel.cxx:152
 TPaveLabel.cxx:153
 TPaveLabel.cxx:154
 TPaveLabel.cxx:155
 TPaveLabel.cxx:156
 TPaveLabel.cxx:157
 TPaveLabel.cxx:158
 TPaveLabel.cxx:159
 TPaveLabel.cxx:160
 TPaveLabel.cxx:161
 TPaveLabel.cxx:162
 TPaveLabel.cxx:163
 TPaveLabel.cxx:164
 TPaveLabel.cxx:165
 TPaveLabel.cxx:166
 TPaveLabel.cxx:167
 TPaveLabel.cxx:168
 TPaveLabel.cxx:169
 TPaveLabel.cxx:170
 TPaveLabel.cxx:171
 TPaveLabel.cxx:172
 TPaveLabel.cxx:173
 TPaveLabel.cxx:174
 TPaveLabel.cxx:175
 TPaveLabel.cxx:176
 TPaveLabel.cxx:177
 TPaveLabel.cxx:178
 TPaveLabel.cxx:179
 TPaveLabel.cxx:180
 TPaveLabel.cxx:181
 TPaveLabel.cxx:182
 TPaveLabel.cxx:183
 TPaveLabel.cxx:184
 TPaveLabel.cxx:185
 TPaveLabel.cxx:186
 TPaveLabel.cxx:187
 TPaveLabel.cxx:188
 TPaveLabel.cxx:189
 TPaveLabel.cxx:190
 TPaveLabel.cxx:191
 TPaveLabel.cxx:192
 TPaveLabel.cxx:193
 TPaveLabel.cxx:194
 TPaveLabel.cxx:195
 TPaveLabel.cxx:196
 TPaveLabel.cxx:197
 TPaveLabel.cxx:198
 TPaveLabel.cxx:199
 TPaveLabel.cxx:200
 TPaveLabel.cxx:201
 TPaveLabel.cxx:202
 TPaveLabel.cxx:203
 TPaveLabel.cxx:204
 TPaveLabel.cxx:205
 TPaveLabel.cxx:206
 TPaveLabel.cxx:207
 TPaveLabel.cxx:208
 TPaveLabel.cxx:209
 TPaveLabel.cxx:210
 TPaveLabel.cxx:211
 TPaveLabel.cxx:212
 TPaveLabel.cxx:213
 TPaveLabel.cxx:214
 TPaveLabel.cxx:215
 TPaveLabel.cxx:216
 TPaveLabel.cxx:217
 TPaveLabel.cxx:218
 TPaveLabel.cxx:219
 TPaveLabel.cxx:220
 TPaveLabel.cxx:221
 TPaveLabel.cxx:222
 TPaveLabel.cxx:223