Logo ROOT  
Reference Guide
TGLabel.cxx
Go to the documentation of this file.
1// @(#)root/gui:$Id$
2// Author: Fons Rademakers 06/01/98
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11/**************************************************************************
12
13 This source is based on Xclass95, a Win95-looking GUI toolkit.
14 Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
15
16 Xclass95 is free software; you can redistribute it and/or
17 modify it under the terms of the GNU Library General Public
18 License as published by the Free Software Foundation; either
19 version 2 of the License, or (at your option) any later version.
20
21**************************************************************************/
22
23//////////////////////////////////////////////////////////////////////////
24// //
25// TGLabel //
26// //
27// This class handles GUI labels. //
28// //
29//////////////////////////////////////////////////////////////////////////
30
31#include "TGLabel.h"
32#include "TGString.h"
33#include "TGWidget.h"
34#include "TGResourcePool.h"
35#include "Riostream.h"
36#include "TColor.h"
37#include "TClass.h"
38#include "TVirtualX.h"
39
40
41const TGFont *TGLabel::fgDefaultFont = nullptr;
42const TGGC *TGLabel::fgDefaultGC = nullptr;
43
45
46////////////////////////////////////////////////////////////////////////////////
47/// Create a label GUI object. TGLabel will become the owner of the
48/// text and will delete it in its dtor.
49
51 FontStruct_t font, UInt_t options, ULong_t back) :
52 TGFrame(p, 1, 1, options, back)
53{
54 fText = text;
59 f3DStyle = 0;
60 fWrapLength = -1;
61 fTFlags = 0;
62 fMLeft = fMRight = fMTop = fMBottom = 0;
63
64 if (!norm) {
65 norm = GetDefaultGC().GetGC();
66 }
67 fNormGC = norm;
68
69 if (!font) {
71 }
72
73 fFont = fClient->GetFontPool()->GetFont(font);
76 &fTWidth, &fTHeight);
77
80 wattr.fBitGravity = 5; // center
81 wattr.fWinGravity = 1;
82 gVirtualX->ChangeWindowAttributes(fId, &wattr);
83
84 Resize();
86}
87
88////////////////////////////////////////////////////////////////////////////////
89/// Create a label GUI object.
90
91TGLabel::TGLabel(const TGWindow *p, const char *text, GContext_t norm,
92 FontStruct_t font, UInt_t options, ULong_t back) :
93 TGFrame(p, 1, 1, options, back)
94{
95 fText = new TGString(!text && !p ? GetName() : text);
100 f3DStyle = 0;
101 fWrapLength = -1;
102 fTFlags = 0;
103 fMLeft = fMRight = fMTop = fMBottom = 0;
104
105 if (!norm) {
106 norm = GetDefaultGC().GetGC();
107 }
108 fNormGC = norm;
109
110 if (!font) {
112 }
113
114 fFont = fClient->GetFontPool()->GetFont(font);
117 &fTWidth, &fTHeight);
118
121 wattr.fBitGravity = 5; // center
122 wattr.fWinGravity = 1;
123 gVirtualX->ChangeWindowAttributes(fId, &wattr);
124
125 Resize();
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Delete label.
131
133{
134 if (fText) {
135 delete fText;
136 }
137
138 if (fHasOwnFont) {
139 TGGCPool *pool = fClient->GetGCPool();
140 TGGC *gc = pool->FindGC(fNormGC);
141 if (gc) pool->FreeGC(gc);
142 }
143
144 if (fFont != fgDefaultFont) {
146 }
147
148 delete fTLayout;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Layout label.
153
155{
156 delete fTLayout;
159 &fTWidth, &fTHeight);
160 fClient->NeedRedraw(this);
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Return default size.
165
167{
170 return TGDimension(w, h);
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Set new text in label. After calling this method one needs to call
175/// the parents frame's Layout() method to force updating of the label size.
176/// The new_text is adopted by the TGLabel and will be properly deleted.
177
179{
180 if (fText) delete fText;
181 fText = new_text;
183
184 Layout();
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Draw text at position (x, y).
189
191{
192 fTLayout->DrawText(fId, gc, x, y, 0, -1);
193 //fTLayout->UnderlineChar(fId, gc, x, y, fText->GetHotPos());
194}
195
196////////////////////////////////////////////////////////////////////////////////
197/// Redraw label widget.
198
200{
201 int x, y;
202
205
206 if (fTMode & kTextLeft) {
207 x = fMLeft;
208 } else if (fTMode & kTextRight) {
209 x = fWidth - fTWidth - fMRight;
210 } else {
211 x = (fWidth - fTWidth + fMLeft - fMRight) >> 1;
212 }
213
214 if (fTMode & kTextTop) {
215 y = 0;
216 } else if (fTMode & kTextBottom) {
217 y = fHeight - fTHeight;
218 } else {
219 y = (fHeight - fTHeight) >> 1;
220 }
221
222
223 if (!fDisabled) {
225 TGGC *gc = pool->FindGC(fNormGC);
226
227 if (!gc) {
229 gc = pool->FindGC(fNormGC);
230 }
231 if (!gc) return;
232
233 switch (f3DStyle) {
234 case kRaisedFrame:
235 case kSunkenFrame:
236 {
237 Pixel_t forecolor = gc->GetForeground();
239 Pixel_t sh = forecolor;
240
241 if (f3DStyle == kRaisedFrame) {
242 Pixel_t t = hi;
243 hi = sh;
244 sh = t;
245 }
246
247 gc->SetForeground(hi);
248 DrawText(gc->GetGC(), x+1, y+1);
249 gc->SetForeground(sh);
250 DrawText(gc->GetGC(), x, y);
251 gc->SetForeground(forecolor);
252 }
253 break;
254
255 default:
256 DrawText(fNormGC, x, y);
257 break;
258 }
259 } else { // disabled
260 FontH_t fontH;
261
263 fontH = gVirtualX->GetFontHandle(fFont->GetFontStruct());
264 } else {
265 fontH = gVirtualX->GetFontHandle(GetDefaultFontStruct());
266 }
267 static TGGC *gc1 = 0;
268 static TGGC *gc2 = 0;
269
270 if (!gc1) {
272 gc1 = new TGGC(*gc1); // copy
273 }
274 gc1->SetFont(fontH);
275 DrawText(gc1->GetGC(), x + 1, y + 1);
276
277 if (!gc2) {
279 gc2 = new TGGC(*gc2); // copy
280 }
281 gc2->SetFont(fontH);
282 DrawText(gc2->GetGC(), x, y);
283 }
284}
285
286////////////////////////////////////////////////////////////////////////////////
287/// Changes text font.
288/// If global is true font is changed globally - otherwise locally.
289
291{
292 TGFont *font = fClient->GetFontPool()->GetFont(fontStruct);
293
294 if (!font) {
295 //error
296 return;
297 }
298
299 SetTextFont(font, global);
300}
301
302////////////////////////////////////////////////////////////////////////////////
303/// Changes text font specified by name.
304/// If global is true font is changed globally - otherwise locally.
305
306void TGLabel::SetTextFont(const char *fontName, Bool_t global)
307{
308 TGFont *font = fClient->GetFont(fontName);
309
310 if (!font) {
311 // error
312 return;
313 }
314
315 SetTextFont(font, global);
316}
317
318////////////////////////////////////////////////////////////////////////////////
319/// Changes text font specified by pointer to TGFont object.
320/// If global is true font is changed globally - otherwise locally.
321
323{
324 if (!font) {
325 //error
326 return;
327 }
328
329 TGFont *oldfont = fFont;
330 fFont = fClient->GetFont(font); // increase usage count
331 if (!fFont) {
332 fFont = oldfont;
333 return;
334 }
335
337 TGGC *gc = pool->FindGC(fNormGC);
338
339 if (!global) {
340 if (gc == &GetDefaultGC() ) { // create new GC
341 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy ctor.
342 }
344 }
345 if (oldfont != fgDefaultFont) {
346 fClient->FreeFont(oldfont);
347 }
348 if (gc) {
350 fNormGC = gc->GetGC();
351 }
353 Layout();
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Changes text color.
358/// If global is true color is changed globally - otherwise locally.
359
361{
363 TGGC *gc = pool->FindGC(fNormGC);
364
365 if (!global) {
366 if (gc == &GetDefaultGC() ) {
367 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy
368 }
370 }
371 if (gc) {
372 gc->SetForeground(color);
373 fNormGC = gc->GetGC();
374 }
375 fClient->NeedRedraw(this);
376}
377
378////////////////////////////////////////////////////////////////////////////////
379/// Changes text color.
380/// If global is true color is changed globally - otherwise locally.
381
383{
384 if (color) {
385 SetTextColor(color->GetPixel(), global);
386 }
387}
388
389////////////////////////////////////////////////////////////////////////////////
390/// Set text justification. Mode is an OR of the bits:
391/// kTextTop, kTextBottom, kTextLeft, kTextRight, kTextCenterX and
392/// kTextCenterY.
393
395{
397 fTMode = mode;
398
401 wattr.fWinGravity = 1;
402
403 switch (mode) {
404 case kTextTop | kTextLeft:
405 wattr.fBitGravity = 1; //NorthWestGravity
406 break;
407 case kTextTop | kTextCenterX:
408 case kTextTop:
409 wattr.fBitGravity = 2; //NorthGravity
410 break;
411 case kTextTop | kTextRight:
412 wattr.fBitGravity = 3; //NorthEastGravity
413 break;
414 case kTextLeft | kTextCenterY:
415 case kTextLeft:
416 wattr.fBitGravity = 4; //WestGravity
417 break;
419 wattr.fBitGravity = 5; //CenterGravity
420 break;
422 case kTextRight:
423 wattr.fBitGravity = 6; //EastGravity
424 break;
425 case kTextBottom | kTextLeft:
426 wattr.fBitGravity = 7; //SouthWestGravity
427 break;
429 case kTextBottom:
430 wattr.fBitGravity = 8; //SouthGravity
431 break;
432 case kTextBottom | kTextRight:
433 wattr.fBitGravity = 9; //SouthEastGravity
434 break;
435 default:
436 wattr.fBitGravity = 5; //CenterGravity
437 break;
438 }
439
440 gVirtualX->ChangeWindowAttributes(fId, &wattr);
441
442 Layout();
443}
444
445////////////////////////////////////////////////////////////////////////////////
446/// Returns kTRUE if text attributes are unique.
447/// Returns kFALSE if text attributes are shared (global).
448
450{
451 return fHasOwnFont;
452}
453
454////////////////////////////////////////////////////////////////////////////////
455/// Save a label widget as a C++ statement(s) on output stream out.
456
457void TGLabel::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
458{
459 char quote = '"';
460
461 // font + GC
462 option = GetName()+5; // unique digit id of the name
463 TString parGC, parFont;
464 parFont.Form("%s::GetDefaultFontStruct()",IsA()->GetName());
465 parGC.Form("%s::GetDefaultGC()()",IsA()->GetName());
466
467 if ((GetDefaultFontStruct() != fFont->GetFontStruct()) || (GetDefaultGC()() != fNormGC)) {
469 if (ufont) {
470 ufont->SavePrimitive(out, option);
471 parFont.Form("ufont->GetFontStruct()");
472 }
473
475 if (userGC) {
476 userGC->SavePrimitive(out, option);
477 parGC.Form("uGC->GetGC()");
478 }
479 }
480
482
483 TString label = GetText()->GetString();
484 label.ReplaceAll("\"","\\\"");
485 label.ReplaceAll("\n","\\n");
486
487 out << " TGLabel *";
488 out << GetName() << " = new TGLabel("<< fParent->GetName()
489 << "," << quote << label << quote;
491 if (!GetOptions()) {
493 if (fNormGC == GetDefaultGC()()) {
494 out <<");" << std::endl;
495 } else {
496 out << "," << parGC.Data() << ");" << std::endl;
497 }
498 } else {
499 out << "," << parGC.Data() << "," << parFont.Data() << ");" << std::endl;
500 }
501 } else {
502 out << "," << parGC.Data() << "," << parFont.Data() << "," << GetOptionString() <<");" << std::endl;
503 }
504 } else {
505 out << "," << parGC.Data() << "," << parFont.Data() << "," << GetOptionString() << ",ucolor);" << std::endl;
506 }
507 if (option && strstr(option, "keep_names"))
508 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
509
510 if (fDisabled)
511 out << " " << GetName() << "->Disable();" << std::endl;
512
513 out << " " << GetName() << "->SetTextJustify(" << GetTextJustify() << ");" << std::endl;
514 out << " " << GetName() << "->SetMargins(" << fMLeft << "," << fMRight << ",";
515 out << fMTop << "," << fMBottom << ");" << std::endl;
516 out << " " << GetName() << "->SetWrapLength(" << fWrapLength << ");" << std::endl;
517
518}
519
520////////////////////////////////////////////////////////////////////////////////
521/// Static returning label default font struct.
522
524{
525 if (!fgDefaultFont) {
526 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
527 }
529}
530
531////////////////////////////////////////////////////////////////////////////////
532/// Static returning label default graphics context.
533
535{
536 if (!fgDefaultGC) {
537 fgDefaultGC = gClient->GetResourcePool()->GetFrameGC();
538 }
539 return *fgDefaultGC;
540}
const Mask_t kWABitGravity
Definition: GuiTypes.h:143
Handle_t FontH_t
Definition: GuiTypes.h:34
const Mask_t kWAWinGravity
Definition: GuiTypes.h:144
@ kRaisedFrame
Definition: GuiTypes.h:384
@ kSunkenFrame
Definition: GuiTypes.h:383
@ kFixedWidth
Definition: GuiTypes.h:387
@ kFixedHeight
Definition: GuiTypes.h:389
Handle_t GContext_t
Definition: GuiTypes.h:37
Handle_t FontStruct_t
Definition: GuiTypes.h:38
ULong_t Pixel_t
Definition: GuiTypes.h:39
#define h(i)
Definition: RSha256.hxx:106
const Bool_t kFALSE
Definition: RtypesCore.h:90
unsigned long ULong_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
#define gClient
Definition: TGClient.h:166
@ kTextCenterX
Definition: TGWidget.h:36
@ kTextLeft
Definition: TGWidget.h:34
@ kTextBottom
Definition: TGWidget.h:38
@ kTextTop
Definition: TGWidget.h:37
@ kTextRight
Definition: TGWidget.h:35
@ kTextCenterY
Definition: TGWidget.h:39
float type_of_call hi(const int &, const int &)
#define gVirtualX
Definition: TVirtualX.h:338
The color creation and management class.
Definition: TColor.h:19
ULong_t GetPixel() const
Return pixel value corresponding to this color.
Definition: TColor.cxx:1437
TGFont * GetFont(const char *font, Bool_t fixedDefault=kTRUE)
Get a font from the font pool.
Definition: TGClient.cxx:348
const TGResourcePool * GetResourcePool() const
Definition: TGClient.h:133
void FreeFont(const TGFont *font)
Free a font.
Definition: TGClient.cxx:364
TGGCPool * GetGCPool() const
Definition: TGClient.h:140
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition: TGClient.cxx:372
TGFontPool * GetFontPool() const
Definition: TGClient.h:145
TGFont * GetFont(const char *font, Bool_t fixedDefault=kTRUE)
Get the specified font.
Definition: TGFont.cxx:1543
void FreeFont(const TGFont *font)
Free font. If ref count is 0 delete font.
Definition: TGFont.cxx:1703
TGFont * FindFont(FontStruct_t font) const
Find font based on its font struct. Returns 0 if font is not found.
Definition: TGFont.cxx:1729
Definition: TGFont.h:149
FontStruct_t GetFontStruct() const
Definition: TGFont.h:193
virtual void SavePrimitive(std::ostream &out, Option_t *="")
Save the used font as a C++ statement(s) on output stream out.
Definition: TGFont.cxx:1869
TGTextLayout * ComputeTextLayout(const char *string, Int_t numChars, Int_t wrapLength, Int_t justify, Int_t flags, UInt_t *width, UInt_t *height) const
Computes the amount of screen space needed to display a multi-line, justified string of text.
Definition: TGFont.cxx:649
FontH_t GetFontHandle() const
Definition: TGFont.h:192
virtual void DoRedraw()
Redraw the frame.
Definition: TGFrame.cxx:414
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:693
UInt_t fHeight
Definition: TGFrame.h:113
static const TGGC & GetHilightGC()
Get highlight color graphics context.
Definition: TGFrame.cxx:739
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:667
virtual UInt_t GetOptions() const
Definition: TGFrame.h:222
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2464
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition: TGFrame.cxx:749
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:589
UInt_t fWidth
Definition: TGFrame.h:112
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2437
Pixel_t fBackground
Definition: TGFrame.h:120
Definition: TGGC.h:112
TGGC * GetGC(GCValues_t *values, Bool_t rw=kFALSE)
Get the best matching graphics context depending on values.
Definition: TGGC.cxx:985
void FreeGC(const TGGC *gc)
Delete graphics context if it is not used anymore.
Definition: TGGC.cxx:916
TGGC * FindGC(const TGGC *gc)
Find graphics context. Returns 0 in case gc is not found.
Definition: TGGC.cxx:949
Definition: TGGC.h:31
GContext_t GetGC() const
Definition: TGGC.h:50
void SavePrimitive(std::ostream &out, Option_t *option="")
Save graphics context info as a C++ statement(s) on output stream out.
Definition: TGGC.cxx:625
void SetFont(FontH_t v)
Set font.
Definition: TGGC.cxx:409
Pixel_t GetForeground() const
Definition: TGGC.h:82
void SetForeground(Pixel_t v)
Set foreground color.
Definition: TGGC.cxx:276
const GCValues_t * GetAttributes() const
Definition: TGGC.h:78
Int_t fMLeft
Definition: TGLabel.h:38
virtual ~TGLabel()
Delete label.
Definition: TGLabel.cxx:132
void SetTextJustify(Int_t tmode)
Set text justification.
Definition: TGLabel.cxx:394
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:360
Int_t f3DStyle
Definition: TGLabel.h:43
Bool_t HasOwnFont() const
Returns kTRUE if text attributes are unique.
Definition: TGLabel.cxx:449
virtual void DrawText(GContext_t gc, Int_t x, Int_t y)
Draw text at position (x, y).
Definition: TGLabel.cxx:190
Bool_t fDisabled
Definition: TGLabel.h:51
static const TGGC * fgDefaultGC
Definition: TGLabel.h:57
GContext_t fNormGC
Definition: TGLabel.h:47
Int_t GetTextJustify() const
Definition: TGLabel.h:90
virtual void Layout()
Layout label.
Definition: TGLabel.cxx:154
virtual void DoRedraw()
Redraw label widget.
Definition: TGLabel.cxx:199
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:178
static const TGGC & GetDefaultGC()
Static returning label default graphics context.
Definition: TGLabel.cxx:534
Bool_t fHasOwnFont
Definition: TGLabel.h:50
UInt_t fTWidth
Definition: TGLabel.h:36
Int_t fMRight
Definition: TGLabel.h:39
TGFont * fFont
Definition: TGLabel.h:48
UInt_t fTHeight
Definition: TGLabel.h:37
Int_t fWrapLength
Definition: TGLabel.h:44
TGLabel(const TGLabel &)
static FontStruct_t GetDefaultFontStruct()
Static returning label default font struct.
Definition: TGLabel.cxx:523
Int_t fMTop
Definition: TGLabel.h:40
Int_t fTMode
Definition: TGLabel.h:42
Bool_t fTextChanged
Definition: TGLabel.h:46
static const TGFont * fgDefaultFont
Definition: TGLabel.h:56
virtual TGDimension GetDefaultSize() const
Return default size.
Definition: TGLabel.cxx:166
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a label widget as a C++ statement(s) on output stream out.
Definition: TGLabel.cxx:457
Int_t fMBottom
Definition: TGLabel.h:41
virtual void SetTextFont(TGFont *font, Bool_t global=kFALSE)
Changes text font specified by pointer to TGFont object.
Definition: TGLabel.cxx:322
Int_t fTFlags
Definition: TGLabel.h:45
TGTextLayout * fTLayout
Definition: TGLabel.h:49
const TGString * GetText() const
Definition: TGLabel.h:82
TGString * fText
Definition: TGLabel.h:35
TGClient * fClient
Definition: TGObject.h:37
Handle_t fId
Definition: TGObject.h:36
TGFontPool * GetFontPool() const
TGGCPool * GetGCPool() const
Int_t GetLength() const
Definition: TGString.h:39
const char * GetString() const
Definition: TGString.h:40
void DrawText(Drawable_t dst, GContext_t gc, Int_t x, Int_t y, Int_t firstChar, Int_t lastChar) const
Use the information in the TGTextLayout object to display a multi-line, justified string of text.
Definition: TGFont.cxx:908
virtual void SetWindowName(const char *name=0)
Set window name.
Definition: TGWindow.cxx:119
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:326
const TGWindow * fParent
Definition: TGWindow.h:36
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
TText * text
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17