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
39
41const TGGC *TGLabel::fgDefaultGC = 0;
42
44
45////////////////////////////////////////////////////////////////////////////////
46/// Create a label GUI object. TGLabel will become the owner of the
47/// text and will delete it in its dtor.
48
50 FontStruct_t font, UInt_t options, ULong_t back) :
51 TGFrame(p, 1, 1, options, back)
52{
53 fText = text;
58 f3DStyle = 0;
59 fWrapLength = -1;
60 fTFlags = 0;
61 fMLeft = fMRight = fMTop = fMBottom = 0;
62
63 if (!norm) {
64 norm = GetDefaultGC().GetGC();
65 }
66 fNormGC = norm;
67
68 if (!font) {
70 }
71
72 fFont = fClient->GetFontPool()->GetFont(font);
75 &fTWidth, &fTHeight);
76
79 wattr.fBitGravity = 5; // center
80 wattr.fWinGravity = 1;
81 gVirtualX->ChangeWindowAttributes(fId, &wattr);
82
83 Resize();
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Create a label GUI object.
89
90TGLabel::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)
93{
94 fText = new TGString(!text && !p ? GetName() : text);
99 f3DStyle = 0;
100 fWrapLength = -1;
101 fTFlags = 0;
102 fMLeft = fMRight = fMTop = fMBottom = 0;
103
104 if (!norm) {
105 norm = GetDefaultGC().GetGC();
106 }
107 fNormGC = norm;
108
109 if (!font) {
111 }
112
113 fFont = fClient->GetFontPool()->GetFont(font);
116 &fTWidth, &fTHeight);
117
120 wattr.fBitGravity = 5; // center
121 wattr.fWinGravity = 1;
122 gVirtualX->ChangeWindowAttributes(fId, &wattr);
123
124 Resize();
126}
127
128////////////////////////////////////////////////////////////////////////////////
129/// Delete label.
130
132{
133 if (fText) {
134 delete fText;
135 }
136
137 if (fHasOwnFont) {
138 TGGCPool *pool = fClient->GetGCPool();
139 TGGC *gc = pool->FindGC(fNormGC);
140 if (gc) pool->FreeGC(gc);
141 }
142
143 if (fFont != fgDefaultFont) {
145 }
146
147 delete fTLayout;
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Layout label.
152
154{
155 delete fTLayout;
158 &fTWidth, &fTHeight);
159 fClient->NeedRedraw(this);
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Return default size.
164
166{
169 return TGDimension(w, h);
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Set new text in label. After calling this method one needs to call
174/// the parents frame's Layout() method to force updating of the label size.
175/// The new_text is adopted by the TGLabel and will be properly deleted.
176
178{
179 if (fText) delete fText;
180 fText = new_text;
182
183 Layout();
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Draw text at position (x, y).
188
190{
191 fTLayout->DrawText(fId, gc, x, y, 0, -1);
192 //fTLayout->UnderlineChar(fId, gc, x, y, fText->GetHotPos());
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Redraw label widget.
197
199{
200 int x, y;
201
204
205 if (fTMode & kTextLeft) {
206 x = fMLeft;
207 } else if (fTMode & kTextRight) {
208 x = fWidth - fTWidth - fMRight;
209 } else {
210 x = (fWidth - fTWidth + fMLeft - fMRight) >> 1;
211 }
212
213 if (fTMode & kTextTop) {
214 y = 0;
215 } else if (fTMode & kTextBottom) {
216 y = fHeight - fTHeight;
217 } else {
218 y = (fHeight - fTHeight) >> 1;
219 }
220
221
222 if (!fDisabled) {
224 TGGC *gc = pool->FindGC(fNormGC);
225
226 if (!gc) {
228 gc = pool->FindGC(fNormGC);
229 }
230 if (!gc) return;
231
232 switch (f3DStyle) {
233 case kRaisedFrame:
234 case kSunkenFrame:
235 {
236 Pixel_t forecolor = gc->GetForeground();
238 Pixel_t sh = forecolor;
239
240 if (f3DStyle == kRaisedFrame) {
241 Pixel_t t = hi;
242 hi = sh;
243 sh = t;
244 }
245
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);
251 }
252 break;
253
254 default:
255 DrawText(fNormGC, x, y);
256 break;
257 }
258 } else { // disabled
259 FontH_t fontH;
260
262 fontH = gVirtualX->GetFontHandle(fFont->GetFontStruct());
263 } else {
264 fontH = gVirtualX->GetFontHandle(GetDefaultFontStruct());
265 }
266 static TGGC *gc1 = 0;
267 static TGGC *gc2 = 0;
268
269 if (!gc1) {
271 gc1 = new TGGC(*gc1); // copy
272 }
273 gc1->SetFont(fontH);
274 DrawText(gc1->GetGC(), x + 1, y + 1);
275
276 if (!gc2) {
278 gc2 = new TGGC(*gc2); // copy
279 }
280 gc2->SetFont(fontH);
281 DrawText(gc2->GetGC(), x, y);
282 }
283}
284
285////////////////////////////////////////////////////////////////////////////////
286/// Changes text font.
287/// If global is true font is changed globally - otherwise locally.
288
290{
291 TGFont *font = fClient->GetFontPool()->GetFont(fontStruct);
292
293 if (!font) {
294 //error
295 return;
296 }
297
298 SetTextFont(font, global);
299}
300
301////////////////////////////////////////////////////////////////////////////////
302/// Changes text font specified by name.
303/// If global is true font is changed globally - otherwise locally.
304
305void TGLabel::SetTextFont(const char *fontName, Bool_t global)
306{
307 TGFont *font = fClient->GetFont(fontName);
308
309 if (!font) {
310 // error
311 return;
312 }
313
314 SetTextFont(font, global);
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// Changes text font specified by pointer to TGFont object.
319/// If global is true font is changed globally - otherwise locally.
320
322{
323 if (!font) {
324 //error
325 return;
326 }
327
328 TGFont *oldfont = fFont;
329 fFont = fClient->GetFont(font); // increase usage count
330 if (!fFont) {
331 fFont = oldfont;
332 return;
333 }
334
336 TGGC *gc = pool->FindGC(fNormGC);
337
338 if (!global) {
339 if (gc == &GetDefaultGC() ) { // create new GC
340 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy ctor.
341 }
343 }
344 if (oldfont != fgDefaultFont) {
345 fClient->FreeFont(oldfont);
346 }
347 if (gc) {
349 fNormGC = gc->GetGC();
350 }
352 Layout();
353}
354
355////////////////////////////////////////////////////////////////////////////////
356/// Changes text color.
357/// If global is true color is changed globally - otherwise locally.
358
360{
362 TGGC *gc = pool->FindGC(fNormGC);
363
364 if (!global) {
365 if (gc == &GetDefaultGC() ) {
366 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy
367 }
369 }
370 if (gc) {
371 gc->SetForeground(color);
372 fNormGC = gc->GetGC();
373 }
374 fClient->NeedRedraw(this);
375}
376
377////////////////////////////////////////////////////////////////////////////////
378/// Changes text color.
379/// If global is true color is changed globally - otherwise locally.
380
382{
383 if (color) {
384 SetTextColor(color->GetPixel(), global);
385 }
386}
387
388////////////////////////////////////////////////////////////////////////////////
389/// Set text justification. Mode is an OR of the bits:
390/// kTextTop, kTextBottom, kTextLeft, kTextRight, kTextCenterX and
391/// kTextCenterY.
392
394{
396 fTMode = mode;
397
400 wattr.fWinGravity = 1;
401
402 switch (mode) {
403 case kTextTop | kTextLeft:
404 wattr.fBitGravity = 1; //NorthWestGravity
405 break;
406 case kTextTop | kTextCenterX:
407 case kTextTop:
408 wattr.fBitGravity = 2; //NorthGravity
409 break;
410 case kTextTop | kTextRight:
411 wattr.fBitGravity = 3; //NorthEastGravity
412 break;
413 case kTextLeft | kTextCenterY:
414 case kTextLeft:
415 wattr.fBitGravity = 4; //WestGravity
416 break;
418 wattr.fBitGravity = 5; //CenterGravity
419 break;
421 case kTextRight:
422 wattr.fBitGravity = 6; //EastGravity
423 break;
424 case kTextBottom | kTextLeft:
425 wattr.fBitGravity = 7; //SouthWestGravity
426 break;
428 case kTextBottom:
429 wattr.fBitGravity = 8; //SouthGravity
430 break;
431 case kTextBottom | kTextRight:
432 wattr.fBitGravity = 9; //SouthEastGravity
433 break;
434 default:
435 wattr.fBitGravity = 5; //CenterGravity
436 break;
437 }
438
439 gVirtualX->ChangeWindowAttributes(fId, &wattr);
440
441 Layout();
442}
443
444////////////////////////////////////////////////////////////////////////////////
445/// Returns kTRUE if text attributes are unique.
446/// Returns kFALSE if text attributes are shared (global).
447
449{
450 return fHasOwnFont;
451}
452
453////////////////////////////////////////////////////////////////////////////////
454/// Save a label widget as a C++ statement(s) on output stream out.
455
456void TGLabel::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
457{
458 char quote = '"';
459
460 // font + GC
461 option = GetName()+5; // unique digit id of the name
462 TString parGC, parFont;
463 parFont.Form("%s::GetDefaultFontStruct()",IsA()->GetName());
464 parGC.Form("%s::GetDefaultGC()()",IsA()->GetName());
465
466 if ((GetDefaultFontStruct() != fFont->GetFontStruct()) || (GetDefaultGC()() != fNormGC)) {
468 if (ufont) {
469 ufont->SavePrimitive(out, option);
470 parFont.Form("ufont->GetFontStruct()");
471 }
472
474 if (userGC) {
475 userGC->SavePrimitive(out, option);
476 parGC.Form("uGC->GetGC()");
477 }
478 }
479
481
482 TString label = GetText()->GetString();
483 label.ReplaceAll("\"","\\\"");
484 label.ReplaceAll("\n","\\n");
485
486 out << " TGLabel *";
487 out << GetName() << " = new TGLabel("<< fParent->GetName()
488 << "," << quote << label << quote;
490 if (!GetOptions()) {
492 if (fNormGC == GetDefaultGC()()) {
493 out <<");" << std::endl;
494 } else {
495 out << "," << parGC.Data() << ");" << std::endl;
496 }
497 } else {
498 out << "," << parGC.Data() << "," << parFont.Data() << ");" << std::endl;
499 }
500 } else {
501 out << "," << parGC.Data() << "," << parFont.Data() << "," << GetOptionString() <<");" << std::endl;
502 }
503 } else {
504 out << "," << parGC.Data() << "," << parFont.Data() << "," << GetOptionString() << ",ucolor);" << std::endl;
505 }
506 if (option && strstr(option, "keep_names"))
507 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
508
509 if (fDisabled)
510 out << " " << GetName() << "->Disable();" << std::endl;
511
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;
516
517}
518
519////////////////////////////////////////////////////////////////////////////////
520/// Static returning label default font struct.
521
523{
524 if (!fgDefaultFont) {
525 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
526 }
528}
529
530////////////////////////////////////////////////////////////////////////////////
531/// Static returning label default graphics context.
532
534{
535 if (!fgDefaultGC) {
536 fgDefaultGC = gClient->GetResourcePool()->GetFrameGC();
537 }
538 return *fgDefaultGC;
539}
const Mask_t kWABitGravity
Definition: GuiTypes.h:143
Handle_t FontH_t
Definition: GuiTypes.h:34
const Mask_t kWAWinGravity
Definition: GuiTypes.h:144
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
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
unsigned long ULong_t
Definition: RtypesCore.h:51
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
#define gClient
Definition: TGClient.h:166
@ kRaisedFrame
Definition: TGFrame.h:62
@ kSunkenFrame
Definition: TGFrame.h:61
@ kFixedWidth
Definition: TGFrame.h:65
@ kFixedHeight
Definition: TGFrame.h:67
@ 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:345
The color creation and management class.
Definition: TColor.h:19
ULong_t GetPixel() const
Return pixel value corresponding to this color.
Definition: TColor.cxx:1434
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:412
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:691
UInt_t fHeight
Definition: TGFrame.h:135
static const TGGC & GetHilightGC()
Get highlight color graphics context.
Definition: TGFrame.cxx:737
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:665
virtual UInt_t GetOptions() const
Definition: TGFrame.h:244
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2462
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition: TGFrame.cxx:747
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
UInt_t fWidth
Definition: TGFrame.h:134
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2435
Pixel_t fBackground
Definition: TGFrame.h:142
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:131
void SetTextJustify(Int_t tmode)
Set text justification.
Definition: TGLabel.cxx:393
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:359
Int_t f3DStyle
Definition: TGLabel.h:43
Bool_t HasOwnFont() const
Returns kTRUE if text attributes are unique.
Definition: TGLabel.cxx:448
virtual void DrawText(GContext_t gc, Int_t x, Int_t y)
Draw text at position (x, y).
Definition: TGLabel.cxx:189
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:153
virtual void DoRedraw()
Redraw label widget.
Definition: TGLabel.cxx:198
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:177
static const TGGC & GetDefaultGC()
Static returning label default graphics context.
Definition: TGLabel.cxx:533
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:522
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:165
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:456
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:321
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:118
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:221
const TGWindow * fParent
Definition: TGWindow.h:37
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