00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "TGLabel.h"
00032 #include "TGString.h"
00033 #include "TGWidget.h"
00034 #include "TGResourcePool.h"
00035 #include "Riostream.h"
00036 #include "TColor.h"
00037 #include "TClass.h"
00038
00039
00040 const TGFont *TGLabel::fgDefaultFont = 0;
00041 const TGGC *TGLabel::fgDefaultGC = 0;
00042
00043 ClassImp(TGLabel)
00044
00045
00046 TGLabel::TGLabel(const TGWindow *p, TGString *text, GContext_t norm,
00047 FontStruct_t font, UInt_t options, ULong_t back) :
00048 TGFrame(p, 1, 1, options, back)
00049 {
00050
00051
00052
00053 fText = text;
00054 fTMode = kTextCenterX | kTextCenterY;
00055 fTextChanged = kTRUE;
00056 fHasOwnFont = kFALSE;
00057 fDisabled = kFALSE;
00058 f3DStyle = 0;
00059 fWrapLength = -1;
00060 fTFlags = 0;
00061 fMLeft = fMRight = fMTop = fMBottom = 0;
00062
00063 if (!norm) {
00064 norm = fgDefaultGC->GetGC();
00065 }
00066 fNormGC = norm;
00067
00068 if (!font) {
00069 font = fgDefaultFont->GetFontStruct();
00070 }
00071
00072 fFont = fClient->GetFontPool()->GetFont(font);
00073 fTLayout = fFont->ComputeTextLayout(fText->GetString(), fText->GetLength(),
00074 fWrapLength, kTextLeft, fTFlags,
00075 &fTWidth, &fTHeight);
00076
00077 SetWindowAttributes_t wattr;
00078 wattr.fMask = kWAWinGravity | kWABitGravity;
00079 wattr.fBitGravity = 5;
00080 wattr.fWinGravity = 1;
00081 gVirtualX->ChangeWindowAttributes(fId, &wattr);
00082
00083 Resize();
00084 SetWindowName();
00085 }
00086
00087
00088 TGLabel::TGLabel(const TGWindow *p, const char *text, GContext_t norm,
00089 FontStruct_t font, UInt_t options, ULong_t back) :
00090 TGFrame(p, 1, 1, options, back)
00091 {
00092
00093
00094 fText = new TGString(!text && !p ? GetName() : text);
00095 fTMode = kTextCenterX | kTextCenterY;
00096 fTextChanged = kTRUE;
00097 fHasOwnFont = kFALSE;
00098 fDisabled = kFALSE;
00099 f3DStyle = 0;
00100 fWrapLength = -1;
00101 fTFlags = 0;
00102 fMLeft = fMRight = fMTop = fMBottom = 0;
00103
00104 if (!norm) {
00105 norm = fgDefaultGC->GetGC();
00106 }
00107 fNormGC = norm;
00108
00109 if (!font) {
00110 font = fgDefaultFont->GetFontStruct();
00111 }
00112
00113 fFont = fClient->GetFontPool()->GetFont(font);
00114 fTLayout = fFont->ComputeTextLayout(fText->GetString(), fText->GetLength(),
00115 fWrapLength, kTextLeft, fTFlags,
00116 &fTWidth, &fTHeight);
00117
00118 SetWindowAttributes_t wattr;
00119 wattr.fMask = kWAWinGravity | kWABitGravity;
00120 wattr.fBitGravity = 5;
00121 wattr.fWinGravity = 1;
00122 gVirtualX->ChangeWindowAttributes(fId, &wattr);
00123
00124 Resize();
00125 SetWindowName();
00126 }
00127
00128
00129 TGLabel::~TGLabel()
00130 {
00131
00132
00133 if (fText) {
00134 delete fText;
00135 }
00136
00137 if (fHasOwnFont) {
00138 TGGCPool *pool = fClient->GetGCPool();
00139 TGGC *gc = pool->FindGC(fNormGC);
00140 if (gc) pool->FreeGC(gc);
00141 }
00142
00143 if (fFont != fgDefaultFont) {
00144 fClient->GetFontPool()->FreeFont(fFont);
00145 }
00146
00147 delete fTLayout;
00148 }
00149
00150
00151 void TGLabel::Layout()
00152 {
00153
00154
00155 delete fTLayout;
00156 fTLayout = fFont->ComputeTextLayout(fText->GetString(), fText->GetLength(),
00157 fWrapLength, kTextLeft, fTFlags,
00158 &fTWidth, &fTHeight);
00159 fClient->NeedRedraw(this);
00160 }
00161
00162
00163 TGDimension TGLabel::GetDefaultSize() const
00164 {
00165
00166
00167 UInt_t w = GetOptions() & kFixedWidth ? fWidth : fTWidth + fMLeft + fMRight;
00168 UInt_t h = GetOptions() & kFixedHeight ? fHeight : fTHeight + fMTop + fMBottom + 1;
00169 return TGDimension(w, h);
00170 }
00171
00172
00173 void TGLabel::SetText(TGString *new_text)
00174 {
00175
00176
00177
00178
00179 if (fText) delete fText;
00180 fText = new_text;
00181 fTextChanged = kTRUE;
00182
00183 Layout();
00184 }
00185
00186
00187 void TGLabel::DrawText(GContext_t gc, Int_t x, Int_t y)
00188 {
00189
00190
00191 fTLayout->DrawText(fId, gc, x, y, 0, -1);
00192
00193 }
00194
00195
00196 void TGLabel::DoRedraw()
00197 {
00198
00199
00200 int x, y;
00201
00202 TGFrame::DoRedraw();
00203 fTextChanged = kFALSE;
00204
00205 if (fTMode & kTextLeft) {
00206 x = fMLeft;
00207 } else if (fTMode & kTextRight) {
00208 x = fWidth - fTWidth - fMRight;
00209 } else {
00210 x = (fWidth - fTWidth + fMLeft - fMRight) >> 1;
00211 }
00212
00213 if (fTMode & kTextTop) {
00214 y = 0;
00215 } else if (fTMode & kTextBottom) {
00216 y = fHeight - fTHeight;
00217 } else {
00218 y = (fHeight - fTHeight) >> 1;
00219 }
00220
00221
00222 if (!fDisabled) {
00223 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
00224 TGGC *gc = pool->FindGC(fNormGC);
00225
00226 if (!gc) {
00227 fNormGC = fgDefaultGC->GetGC();
00228 gc = pool->FindGC(fNormGC);
00229 }
00230 if (!gc) return;
00231
00232 switch (f3DStyle) {
00233 case kRaisedFrame:
00234 case kSunkenFrame:
00235 {
00236 Pixel_t forecolor = gc->GetForeground();
00237 Pixel_t hi = TGFrame::GetWhitePixel();
00238 Pixel_t sh = forecolor;
00239
00240 if (f3DStyle == kRaisedFrame) {
00241 Pixel_t t = hi;
00242 hi = sh;
00243 sh = t;
00244 }
00245
00246 gc->SetForeground(hi);
00247 DrawText(gc->GetGC(), x+1, y+1);
00248 gc->SetForeground(sh);
00249 DrawText(gc->GetGC(), x, y);
00250 gc->SetForeground(forecolor);
00251 }
00252 break;
00253
00254 default:
00255 DrawText(fNormGC, x, y);
00256 break;
00257 }
00258 } else {
00259 FontH_t fontH;
00260
00261 if (GetDefaultFontStruct() != fFont->GetFontStruct()) {
00262 fontH = gVirtualX->GetFontHandle(fFont->GetFontStruct());
00263 } else {
00264 fontH = gVirtualX->GetFontHandle(GetDefaultFontStruct());
00265 }
00266 static TGGC *gc1 = 0;
00267 static TGGC *gc2 = 0;
00268
00269 if (!gc1) {
00270 gc1 = fClient->GetResourcePool()->GetGCPool()->FindGC(GetHilightGC()());
00271 gc1 = new TGGC(*gc1);
00272 }
00273 gc1->SetFont(fontH);
00274 DrawText(gc1->GetGC(), x + 1, y + 1);
00275
00276 if (!gc2) {
00277 gc2 = fClient->GetResourcePool()->GetGCPool()->FindGC(GetShadowGC()());
00278 gc2 = new TGGC(*gc2);
00279 }
00280 gc2->SetFont(fontH);
00281 DrawText(gc2->GetGC(), x, y);
00282 }
00283 }
00284
00285
00286 void TGLabel::SetTextFont(FontStruct_t fontStruct, Bool_t global)
00287 {
00288
00289
00290
00291 TGFont *font = fClient->GetFontPool()->GetFont(fontStruct);
00292
00293 if (!font) {
00294
00295 return;
00296 }
00297
00298 SetTextFont(font, global);
00299 }
00300
00301
00302 void TGLabel::SetTextFont(const char *fontName, Bool_t global)
00303 {
00304
00305
00306
00307 TGFont *font = fClient->GetFont(fontName);
00308
00309 if (!font) {
00310
00311 return;
00312 }
00313
00314 SetTextFont(font, global);
00315 }
00316
00317
00318 void TGLabel::SetTextFont(TGFont *font, Bool_t global)
00319 {
00320
00321
00322
00323 if (!font) {
00324
00325 return;
00326 }
00327
00328 TGFont *oldfont = fFont;
00329 fFont = fClient->GetFont(font);
00330
00331 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
00332 TGGC *gc = pool->FindGC(fNormGC);
00333
00334 if (!global) {
00335 if (gc == fgDefaultGC) {
00336 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE);
00337 }
00338 fHasOwnFont = kTRUE;
00339 }
00340
00341 if (oldfont != fgDefaultFont) {
00342 fClient->FreeFont(oldfont);
00343 }
00344
00345 gc->SetFont(fFont->GetFontHandle());
00346 fNormGC = gc->GetGC();
00347
00348 fTextChanged = kTRUE;
00349 Layout();
00350 }
00351
00352
00353 void TGLabel::SetTextColor(Pixel_t color, Bool_t global)
00354 {
00355
00356
00357
00358 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
00359 TGGC *gc = pool->FindGC(fNormGC);
00360
00361 if (!global) {
00362 if (gc == fgDefaultGC) {
00363 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE);
00364 }
00365 fHasOwnFont = kTRUE;
00366 }
00367
00368 gc->SetForeground(color);
00369 fNormGC = gc->GetGC();
00370
00371 fClient->NeedRedraw(this);
00372 }
00373
00374
00375 void TGLabel::SetTextColor(TColor *color, Bool_t global)
00376 {
00377
00378
00379
00380 if (color) {
00381 SetTextColor(color->GetPixel(), global);
00382 }
00383 }
00384
00385
00386 void TGLabel::SetTextJustify(Int_t mode)
00387 {
00388
00389
00390
00391
00392 fTextChanged = kTRUE;
00393 fTMode = mode;
00394
00395 SetWindowAttributes_t wattr;
00396 wattr.fMask = kWAWinGravity | kWABitGravity;
00397 wattr.fWinGravity = 1;
00398
00399 switch (mode) {
00400 case kTextTop | kTextLeft:
00401 wattr.fBitGravity = 1;
00402 break;
00403 case kTextTop | kTextCenterX:
00404 case kTextTop:
00405 wattr.fBitGravity = 2;
00406 break;
00407 case kTextTop | kTextRight:
00408 wattr.fBitGravity = 3;
00409 break;
00410 case kTextLeft | kTextCenterY:
00411 case kTextLeft:
00412 wattr.fBitGravity = 4;
00413 break;
00414 case kTextCenterY | kTextCenterX:
00415 wattr.fBitGravity = 5;
00416 break;
00417 case kTextRight | kTextCenterY:
00418 case kTextRight:
00419 wattr.fBitGravity = 6;
00420 break;
00421 case kTextBottom | kTextLeft:
00422 wattr.fBitGravity = 7;
00423 break;
00424 case kTextBottom | kTextCenterX:
00425 case kTextBottom:
00426 wattr.fBitGravity = 8;
00427 break;
00428 case kTextBottom | kTextRight:
00429 wattr.fBitGravity = 9;
00430 break;
00431 default:
00432 wattr.fBitGravity = 5;
00433 break;
00434 }
00435
00436 gVirtualX->ChangeWindowAttributes(fId, &wattr);
00437
00438 Layout();
00439 }
00440
00441
00442 Bool_t TGLabel::HasOwnFont() const
00443 {
00444
00445
00446
00447 return fHasOwnFont;
00448 }
00449
00450
00451 void TGLabel::SavePrimitive(ostream &out, Option_t *option )
00452 {
00453
00454
00455 char quote = '"';
00456
00457
00458 option = GetName()+5;
00459 TString parGC, parFont;
00460 parFont.Form("%s::GetDefaultFontStruct()",IsA()->GetName());
00461 parGC.Form("%s::GetDefaultGC()()",IsA()->GetName());
00462
00463 if ((GetDefaultFontStruct() != fFont->GetFontStruct()) || (GetDefaultGC()() != fNormGC)) {
00464 TGFont *ufont = fClient->GetResourcePool()->GetFontPool()->FindFont(fFont->GetFontStruct());
00465 if (ufont) {
00466 ufont->SavePrimitive(out, option);
00467 parFont.Form("ufont->GetFontStruct()");
00468 }
00469
00470 TGGC *userGC = fClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
00471 if (userGC) {
00472 userGC->SavePrimitive(out, option);
00473 parGC.Form("uGC->GetGC()");
00474 }
00475 }
00476
00477 if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
00478
00479 TString label = GetText()->GetString();
00480 label.ReplaceAll("\"","\\\"");
00481 label.ReplaceAll("\n","\\n");
00482
00483 out << " TGLabel *";
00484 out << GetName() << " = new TGLabel("<< fParent->GetName()
00485 << "," << quote << label << quote;
00486 if (fBackground == GetDefaultFrameBackground()) {
00487 if (!GetOptions()) {
00488 if (fFont->GetFontStruct() == GetDefaultFontStruct()) {
00489 if (fNormGC == GetDefaultGC()()) {
00490 out <<");" << endl;
00491 } else {
00492 out << "," << parGC.Data() << ");" << endl;
00493 }
00494 } else {
00495 out << "," << parGC.Data() << "," << parFont.Data() << ");" << endl;
00496 }
00497 } else {
00498 out << "," << parGC.Data() << "," << parFont.Data() << "," << GetOptionString() <<");" << endl;
00499 }
00500 } else {
00501 out << "," << parGC.Data() << "," << parFont.Data() << "," << GetOptionString() << ",ucolor);" << endl;
00502 }
00503 if (option && strstr(option, "keep_names"))
00504 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00505
00506 if (fDisabled)
00507 out << " " << GetName() << "->Disable();" << endl;
00508
00509 out << " " << GetName() << "->SetTextJustify(" << GetTextJustify() << ");" << endl;
00510 out << " " << GetName() << "->SetMargins(" << fMLeft << "," << fMRight << ",";
00511 out << fMTop << "," << fMBottom << ");" << endl;
00512 out << " " << GetName() << "->SetWrapLength(" << fWrapLength << ");" << endl;
00513
00514 }
00515
00516
00517 FontStruct_t TGLabel::GetDefaultFontStruct()
00518 {
00519
00520
00521 if (!fgDefaultFont) {
00522 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
00523 }
00524 return fgDefaultFont->GetFontStruct();
00525 }
00526
00527
00528 const TGGC &TGLabel::GetDefaultGC()
00529 {
00530
00531
00532 if (!fgDefaultGC) {
00533 fgDefaultGC = gClient->GetResourcePool()->GetFrameGC();
00534 }
00535 return *fgDefaultGC;
00536 }