Text Box

Hi,

is it possible to draw a TText with a bounding box around it?

You get something similar with TPaveLabel - but I want something where you can just specify the x/y value of the text, and you automatically get a box around it (unlike TPaveLabel where you specify x1/y1, x2/y2 and it scales the text to fill the box)

thanks!
Peter

TPavelabel is meant to provide what your are looking for. is there a special case you can show in a small example where a such functionality is needed ?

Hi ,

I’ve attached a macro with two functions which hopefully illustrate where I’m getting stuck. The first (example1) draws two pave labels, with different text. How do I ensure that the pave is automatically sized such that the text size in each is the same?

The second function (example2) shows how I might try and do this using a TText and TBox, but the problem is I don’t know how big to make the box so that the text is nicely contained.

Part of the reason I want to be able to do this is because I want to draw a line connecting the boxes - thus I need to know the box coordinates (which both methods satisfy)- and also have the text sizes the same!

thanks
Peter
example1.C (1.19 KB)

I think the method TText::GetBoundingBox
root.cern.ch/root/html526/src/TT … tml#nPalgC
does what you are looking for.
The code

[code]void example2(){
TCanvas *c = new TCanvas(“canvas”,“title”, 600,400);
//coordinates of first text box
float x_1=0.2;
float y_1=0.2;

//coordinates of second text box
float x_2=0.7;
float y_2=0.7;
TText scale1 (0,0,“label 1”);
scale1.SetTextSizePixels(20);
UInt_t w;
UInt_t h;
scale1.Modify();
scale1.GetBoundingBox(w, h); // this method is “protected” by “nature”.
// To get the correct result one has to call “Modify” first

TPaveText *text1 = new TPaveText(x_1, y_1, x_1+float(w)/gPad->GetWw(), y_1+float(h)/gPad->GetWh());
text1->AddText(“label 1”);
text1->SetTextSizePixels(20);
TText scale2 (0,0,“second label”);
scale2.SetTextSizePixels(20);
scale2.Modify();
scale2.GetBoundingBox(w, h); // this method is “protected” by “nature”.
// To get the correct result one has to call “Modify” first

TPaveText *text2 = new TPaveText(x_2, y_2, x_2+float(w)/gPad->GetWw(), y_2+float(h)/gPad->GetWh());
text2->AddText(“second label”);
text2->SetTextSizePixels(20);

//draw them
text1->Draw();
text2->Draw();
//connect them
TLine *line = new TLine(x_1+0.1, y_1+0.1, x_2, y_2);
line->Draw();
}[/code]shows that. However, one has to be very careful. Even though the method “GetBoundingBox” is declared as “public” it is “protected” by “implementation” :unamused: . Try to comment out the “magic” Modify to see what I mean.

[quote]
How do I ensure that the pave is automatically sized such that the text size in each is the same? [/quote]

  label1->SetTextSize(0.2);
  label2->SetTextSize(0.2);

Sirs,

thank you for your help - Valeri’s approach was exactly what I wanted.

Yes, you still can use TPaveLabel class. One can replace the 2 lines: TPaveText *text1 = new TPaveText(x_1, y_1, x_1+float(w)/gPad->GetWw(), y_1+float(h)/gPad->GetWh()); text1->AddText("label 1"); with 1 line TPaveLabel *text1 = new TPaveLabel(x_1, y_1, x_1+float(w)/gPad->GetWw(), y_1+float(h)/gPad->GetWh(),"label 1");