40 const TGFont *TGLabel::fgDefaultFont = 0;
41 const TGGC *TGLabel::fgDefaultGC = 0;
49 TGLabel::TGLabel(
const TGWindow *p, TGString *text, GContext_t norm,
50 FontStruct_t font, UInt_t options, ULong_t back) :
51 TGFrame(p, 1, 1, options, back)
54 fTMode = kTextCenterX | kTextCenterY;
61 fMLeft = fMRight = fMTop = fMBottom = 0;
64 norm = GetDefaultGC().GetGC();
69 font = fgDefaultFont->GetFontStruct();
72 fFont = fClient->GetFontPool()->GetFont(font);
73 fTLayout = fFont->ComputeTextLayout(fText->GetString(), fText->GetLength(),
74 fWrapLength, kTextLeft, fTFlags,
77 SetWindowAttributes_t wattr;
78 wattr.fMask = kWAWinGravity | kWABitGravity;
79 wattr.fBitGravity = 5;
80 wattr.fWinGravity = 1;
81 gVirtualX->ChangeWindowAttributes(fId, &wattr);
90 TGLabel::TGLabel(
const TGWindow *p,
const char *text, GContext_t norm,
91 FontStruct_t font, UInt_t options, ULong_t back) :
92 TGFrame(p, 1, 1, options, back)
94 fText =
new TGString(!text && !p ? GetName() : text);
95 fTMode = kTextCenterX | kTextCenterY;
102 fMLeft = fMRight = fMTop = fMBottom = 0;
105 norm = GetDefaultGC().GetGC();
110 font = fgDefaultFont->GetFontStruct();
113 fFont = fClient->GetFontPool()->GetFont(font);
114 fTLayout = fFont->ComputeTextLayout(fText->GetString(), fText->GetLength(),
115 fWrapLength, kTextLeft, fTFlags,
116 &fTWidth, &fTHeight);
118 SetWindowAttributes_t wattr;
119 wattr.fMask = kWAWinGravity | kWABitGravity;
120 wattr.fBitGravity = 5;
121 wattr.fWinGravity = 1;
122 gVirtualX->ChangeWindowAttributes(fId, &wattr);
138 TGGCPool *pool = fClient->GetGCPool();
139 TGGC *gc = pool->FindGC(fNormGC);
140 if (gc) pool->FreeGC(gc);
143 if (fFont != fgDefaultFont) {
144 fClient->GetFontPool()->FreeFont(fFont);
153 void TGLabel::Layout()
156 fTLayout = fFont->ComputeTextLayout(fText->GetString(), fText->GetLength(),
157 fWrapLength, kTextLeft, fTFlags,
158 &fTWidth, &fTHeight);
159 fClient->NeedRedraw(
this);
165 TGDimension TGLabel::GetDefaultSize()
const
167 UInt_t w = GetOptions() & kFixedWidth ? fWidth : fTWidth + fMLeft + fMRight;
168 UInt_t h = GetOptions() & kFixedHeight ? fHeight : fTHeight + fMTop + fMBottom + 1;
169 return TGDimension(w, h);
177 void TGLabel::SetText(TGString *new_text)
179 if (fText)
delete fText;
181 fTextChanged = kTRUE;
189 void TGLabel::DrawText(GContext_t gc, Int_t x, Int_t y)
191 fTLayout->DrawText(fId, gc, x, y, 0, -1);
198 void TGLabel::DoRedraw()
203 fTextChanged = kFALSE;
205 if (fTMode & kTextLeft) {
207 }
else if (fTMode & kTextRight) {
208 x = fWidth - fTWidth - fMRight;
210 x = (fWidth - fTWidth + fMLeft - fMRight) >> 1;
213 if (fTMode & kTextTop) {
215 }
else if (fTMode & kTextBottom) {
216 y = fHeight - fTHeight;
218 y = (fHeight - fTHeight) >> 1;
223 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
224 TGGC *gc = pool->FindGC(fNormGC);
227 fNormGC = GetDefaultGC().GetGC();
228 gc = pool->FindGC(fNormGC);
236 Pixel_t forecolor = gc->GetForeground();
237 Pixel_t hi = TGFrame::GetWhitePixel();
238 Pixel_t sh = forecolor;
240 if (f3DStyle == kRaisedFrame) {
246 gc->SetForeground(hi);
247 DrawText(gc->GetGC(), x+1, y+1);
248 gc->SetForeground(sh);
249 DrawText(gc->GetGC(), x, y);
250 gc->SetForeground(forecolor);
255 DrawText(fNormGC, x, y);
261 if (GetDefaultFontStruct() != fFont->GetFontStruct()) {
262 fontH = gVirtualX->GetFontHandle(fFont->GetFontStruct());
264 fontH = gVirtualX->GetFontHandle(GetDefaultFontStruct());
266 static TGGC *gc1 = 0;
267 static TGGC *gc2 = 0;
270 gc1 = fClient->GetResourcePool()->GetGCPool()->FindGC(GetHilightGC()());
271 gc1 =
new TGGC(*gc1);
274 DrawText(gc1->GetGC(), x + 1, y + 1);
277 gc2 = fClient->GetResourcePool()->GetGCPool()->FindGC(GetShadowGC()());
278 gc2 =
new TGGC(*gc2);
281 DrawText(gc2->GetGC(), x, y);
289 void TGLabel::SetTextFont(FontStruct_t fontStruct, Bool_t global)
291 TGFont *font = fClient->GetFontPool()->GetFont(fontStruct);
298 SetTextFont(font, global);
305 void TGLabel::SetTextFont(
const char *fontName, Bool_t global)
307 TGFont *font = fClient->GetFont(fontName);
314 SetTextFont(font, global);
321 void TGLabel::SetTextFont(TGFont *font, Bool_t global)
328 TGFont *oldfont = fFont;
329 fFont = fClient->GetFont(font);
335 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
336 TGGC *gc = pool->FindGC(fNormGC);
339 if (gc == &GetDefaultGC() ) {
340 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE);
344 if (oldfont != fgDefaultFont) {
345 fClient->FreeFont(oldfont);
348 gc->SetFont(fFont->GetFontHandle());
349 fNormGC = gc->GetGC();
351 fTextChanged = kTRUE;
359 void TGLabel::SetTextColor(Pixel_t color, Bool_t global)
361 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
362 TGGC *gc = pool->FindGC(fNormGC);
365 if (gc == &GetDefaultGC() ) {
366 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE);
371 gc->SetForeground(color);
372 fNormGC = gc->GetGC();
374 fClient->NeedRedraw(
this);
381 void TGLabel::SetTextColor(TColor *color, Bool_t global)
384 SetTextColor(color->GetPixel(), global);
393 void TGLabel::SetTextJustify(Int_t mode)
395 fTextChanged = kTRUE;
398 SetWindowAttributes_t wattr;
399 wattr.fMask = kWAWinGravity | kWABitGravity;
400 wattr.fWinGravity = 1;
403 case kTextTop | kTextLeft:
404 wattr.fBitGravity = 1;
406 case kTextTop | kTextCenterX:
408 wattr.fBitGravity = 2;
410 case kTextTop | kTextRight:
411 wattr.fBitGravity = 3;
413 case kTextLeft | kTextCenterY:
415 wattr.fBitGravity = 4;
417 case kTextCenterY | kTextCenterX:
418 wattr.fBitGravity = 5;
420 case kTextRight | kTextCenterY:
422 wattr.fBitGravity = 6;
424 case kTextBottom | kTextLeft:
425 wattr.fBitGravity = 7;
427 case kTextBottom | kTextCenterX:
429 wattr.fBitGravity = 8;
431 case kTextBottom | kTextRight:
432 wattr.fBitGravity = 9;
435 wattr.fBitGravity = 5;
439 gVirtualX->ChangeWindowAttributes(fId, &wattr);
448 Bool_t TGLabel::HasOwnFont()
const
456 void TGLabel::SavePrimitive(std::ostream &out, Option_t *option )
461 option = GetName()+5;
462 TString parGC, parFont;
463 parFont.Form(
"%s::GetDefaultFontStruct()",IsA()->GetName());
464 parGC.Form(
"%s::GetDefaultGC()()",IsA()->GetName());
466 if ((GetDefaultFontStruct() != fFont->GetFontStruct()) || (GetDefaultGC()() != fNormGC)) {
467 TGFont *ufont = fClient->GetResourcePool()->GetFontPool()->FindFont(fFont->GetFontStruct());
469 ufont->SavePrimitive(out, option);
470 parFont.Form(
"ufont->GetFontStruct()");
473 TGGC *userGC = fClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
475 userGC->SavePrimitive(out, option);
476 parGC.Form(
"uGC->GetGC()");
480 if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
482 TString label = GetText()->GetString();
483 label.ReplaceAll(
"\"",
"\\\"");
484 label.ReplaceAll(
"\n",
"\\n");
487 out << GetName() <<
" = new TGLabel("<< fParent->GetName()
488 <<
"," << quote << label << quote;
489 if (fBackground == GetDefaultFrameBackground()) {
491 if (fFont->GetFontStruct() == GetDefaultFontStruct()) {
492 if (fNormGC == GetDefaultGC()()) {
493 out <<
");" << std::endl;
495 out <<
"," << parGC.Data() <<
");" << std::endl;
498 out <<
"," << parGC.Data() <<
"," << parFont.Data() <<
");" << std::endl;
501 out <<
"," << parGC.Data() <<
"," << parFont.Data() <<
"," << GetOptionString() <<
");" << std::endl;
504 out <<
"," << parGC.Data() <<
"," << parFont.Data() <<
"," << GetOptionString() <<
",ucolor);" << std::endl;
506 if (option && strstr(option,
"keep_names"))
507 out <<
" " << GetName() <<
"->SetName(\"" << GetName() <<
"\");" << std::endl;
510 out <<
" " << GetName() <<
"->Disable();" << std::endl;
512 out <<
" " << GetName() <<
"->SetTextJustify(" << GetTextJustify() <<
");" << std::endl;
513 out <<
" " << GetName() <<
"->SetMargins(" << fMLeft <<
"," << fMRight <<
",";
514 out << fMTop <<
"," << fMBottom <<
");" << std::endl;
515 out <<
" " << GetName() <<
"->SetWrapLength(" << fWrapLength <<
");" << std::endl;
522 FontStruct_t TGLabel::GetDefaultFontStruct()
524 if (!fgDefaultFont) {
525 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
527 return fgDefaultFont->GetFontStruct();
533 const TGGC &TGLabel::GetDefaultGC()
536 fgDefaultGC = gClient->GetResourcePool()->GetFrameGC();