Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/** \class TGLabel
25 \ingroup guiwidgets
26
27This class handles GUI labels.
28
29*/
30
31
32#include "TGLabel.h"
33#include "TGString.h"
34#include "TGWidget.h"
35#include "TGResourcePool.h"
36#include "TColor.h"
37#include "TClass.h"
38#include "TVirtualX.h"
39
40#include <iostream>
41
42
43const TGFont *TGLabel::fgDefaultFont = nullptr;
44const TGGC *TGLabel::fgDefaultGC = nullptr;
45
46
47////////////////////////////////////////////////////////////////////////////////
48/// Create a label GUI object. TGLabel will become the owner of the
49/// text and will delete it in its dtor.
50
52 FontStruct_t font, UInt_t options, Pixel_t back) :
53 TGFrame(p, 1, 1, options, back)
54{
55 fText = text;
60 f3DStyle = 0;
61 fWrapLength = -1;
62 fTFlags = 0;
63 fMLeft = fMRight = fMTop = fMBottom = 0;
64
65 if (!norm) {
66 norm = GetDefaultGC().GetGC();
67 }
68 fNormGC = norm;
69
70 if (!font) {
71 font = fgDefaultFont->GetFontStruct();
72 }
73
74 fFont = fClient->GetFontPool()->GetFont(font);
77 &fTWidth, &fTHeight);
78
81 wattr.fBitGravity = 5; // center
82 wattr.fWinGravity = 1;
83 gVirtualX->ChangeWindowAttributes(fId, &wattr);
84
85 Resize();
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Create a label GUI object.
91
93 FontStruct_t font, UInt_t options, Pixel_t back) :
94 TGFrame(p, 1, 1, options, back)
95{
96 fText = new TGString(!text && !p ? GetName() : text);
101 f3DStyle = 0;
102 fWrapLength = -1;
103 fTFlags = 0;
104 fMLeft = fMRight = fMTop = fMBottom = 0;
105
106 if (!norm) {
107 norm = GetDefaultGC().GetGC();
108 }
109 fNormGC = norm;
110
111 if (!font) {
112 font = fgDefaultFont->GetFontStruct();
113 }
114
115 fFont = fClient->GetFontPool()->GetFont(font);
118 &fTWidth, &fTHeight);
119
122 wattr.fBitGravity = 5; // center
123 wattr.fWinGravity = 1;
124 gVirtualX->ChangeWindowAttributes(fId, &wattr);
125
126 Resize();
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Delete label.
132
134{
135 if (fText) {
136 delete fText;
137 }
138
139 if (fHasOwnFont) {
141 TGGC *gc = pool->FindGC(fNormGC);
142 if (gc) pool->FreeGC(gc);
143 }
144
145 if (fFont != fgDefaultFont) {
147 }
148
149 delete fTLayout;
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Layout label.
154
163
164////////////////////////////////////////////////////////////////////////////////
165/// Return default size.
166
173
174////////////////////////////////////////////////////////////////////////////////
175/// Set new text in label. After calling this method one needs to call
176/// the parents frame's Layout() method to force updating of the label size.
177/// The new_text is adopted by the TGLabel and will be properly deleted.
178
180{
181 if (fText) delete fText;
182 fText = new_text;
184
185 Layout();
186}
187
188////////////////////////////////////////////////////////////////////////////////
189/// Draw text at position (x, y).
190
192{
193 fTLayout->DrawText(fId, gc, x, y, 0, -1);
194 //fTLayout->UnderlineChar(fId, gc, x, y, fText->GetHotPos());
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Redraw label widget.
199
201{
202 int x, y;
203
206
207 if (fTMode & kTextLeft) {
208 x = fMLeft;
209 } else if (fTMode & kTextRight) {
210 x = fWidth - fTWidth - fMRight;
211 } else {
212 x = (fWidth - fTWidth + fMLeft - fMRight) >> 1;
213 }
214
215 if (fTMode & kTextTop) {
216 y = 0;
217 } else if (fTMode & kTextBottom) {
218 y = fHeight - fTHeight;
219 } else {
220 y = (fHeight - fTHeight) >> 1;
221 }
222
223
224 if (!fDisabled) {
225 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
226 TGGC *gc = pool->FindGC(fNormGC);
227
228 if (!gc) {
229 fNormGC = GetDefaultGC().GetGC();
230 gc = pool->FindGC(fNormGC);
231 }
232 if (!gc) return;
233
234 switch (f3DStyle) {
235 case kRaisedFrame:
236 case kSunkenFrame:
237 {
238 Pixel_t forecolor = gc->GetForeground();
241
242 if (f3DStyle == kRaisedFrame) {
243 Pixel_t t = hi;
244 hi = sh;
245 sh = t;
246 }
247
248 gc->SetForeground(hi);
249 DrawText(gc->GetGC(), x+1, y+1);
250 gc->SetForeground(sh);
251 DrawText(gc->GetGC(), x, y);
252 gc->SetForeground(forecolor);
253 }
254 break;
255
256 default:
257 DrawText(fNormGC, x, y);
258 break;
259 }
260 } else { // disabled
262
264 fontH = gVirtualX->GetFontHandle(fFont->GetFontStruct());
265 } else {
266 fontH = gVirtualX->GetFontHandle(GetDefaultFontStruct());
267 }
268 static TGGC *gc1 = 0;
269 static TGGC *gc2 = 0;
270
271 if (!gc1) {
272 gc1 = fClient->GetResourcePool()->GetGCPool()->FindGC(GetHilightGC()());
273 gc1 = new TGGC(*gc1); // copy
274 }
275 gc1->SetFont(fontH);
276 DrawText(gc1->GetGC(), x + 1, y + 1);
277
278 if (!gc2) {
279 gc2 = fClient->GetResourcePool()->GetGCPool()->FindGC(GetShadowGC()());
280 gc2 = new TGGC(*gc2); // copy
281 }
282 gc2->SetFont(fontH);
283 DrawText(gc2->GetGC(), x, y);
284 }
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// Changes text font.
289/// If global is true font is changed globally - otherwise locally.
290
292{
294
295 if (!font) {
296 //error
297 return;
298 }
299
300 SetTextFont(font, global);
301}
302
303////////////////////////////////////////////////////////////////////////////////
304/// Changes text font specified by name.
305/// If global is true font is changed globally - otherwise locally.
306
308{
309 TGFont *font = fClient->GetFont(fontName);
310
311 if (!font) {
312 // error
313 return;
314 }
315
316 SetTextFont(font, global);
317}
318
319////////////////////////////////////////////////////////////////////////////////
320/// Changes text font specified by pointer to TGFont object.
321/// If global is true font is changed globally - otherwise locally.
322
324{
325 if (!font) {
326 //error
327 return;
328 }
329
331 fFont = fClient->GetFont(font); // increase usage count
332 if (!fFont) {
333 fFont = oldfont;
334 return;
335 }
336
337 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
338 TGGC *gc = pool->FindGC(fNormGC);
339
340 if (!global) {
341 if (gc == &GetDefaultGC() ) { // create new GC
342 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy ctor.
343 }
345 }
346 if (oldfont != fgDefaultFont) {
348 }
349 if (gc) {
350 gc->SetFont(fFont->GetFontHandle());
351 fNormGC = gc->GetGC();
352 }
354 Layout();
355}
356
357////////////////////////////////////////////////////////////////////////////////
358/// Changes text color.
359/// If global is true color is changed globally - otherwise locally.
360
362{
363 TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
364 TGGC *gc = pool->FindGC(fNormGC);
365
366 if (!global) {
367 if (gc == &GetDefaultGC() ) {
368 gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy
369 }
371 }
372 if (gc) {
373 gc->SetForeground(color);
374 fNormGC = gc->GetGC();
375 }
376 fClient->NeedRedraw(this);
377}
378
379////////////////////////////////////////////////////////////////////////////////
380/// Changes text color.
381/// If global is true color is changed globally - otherwise locally.
382
384{
385 if (color) {
386 SetTextColor(color->GetPixel(), global);
387 }
388}
389
390////////////////////////////////////////////////////////////////////////////////
391/// Set text justification. Mode is an OR of the bits:
392/// kTextTop, kTextBottom, kTextLeft, kTextRight, kTextCenterX and
393/// kTextCenterY.
394
396{
398 fTMode = mode;
399
402 wattr.fWinGravity = 1;
403
404 switch (mode) {
405 case kTextTop | kTextLeft:
406 wattr.fBitGravity = 1; //NorthWestGravity
407 break;
408 case kTextTop | kTextCenterX:
409 case kTextTop:
410 wattr.fBitGravity = 2; //NorthGravity
411 break;
412 case kTextTop | kTextRight:
413 wattr.fBitGravity = 3; //NorthEastGravity
414 break;
415 case kTextLeft | kTextCenterY:
416 case kTextLeft:
417 wattr.fBitGravity = 4; //WestGravity
418 break;
420 wattr.fBitGravity = 5; //CenterGravity
421 break;
423 case kTextRight:
424 wattr.fBitGravity = 6; //EastGravity
425 break;
426 case kTextBottom | kTextLeft:
427 wattr.fBitGravity = 7; //SouthWestGravity
428 break;
430 case kTextBottom:
431 wattr.fBitGravity = 8; //SouthGravity
432 break;
433 case kTextBottom | kTextRight:
434 wattr.fBitGravity = 9; //SouthEastGravity
435 break;
436 default:
437 wattr.fBitGravity = 5; //CenterGravity
438 break;
439 }
440
441 gVirtualX->ChangeWindowAttributes(fId, &wattr);
442
443 Layout();
444}
445
446////////////////////////////////////////////////////////////////////////////////
447/// Returns kTRUE if text attributes are unique.
448/// Returns kFALSE if text attributes are shared (global).
449
451{
452 return fHasOwnFont;
453}
454
455////////////////////////////////////////////////////////////////////////////////
456/// Save a label widget as a C++ statement(s) on output stream out.
457
458void TGLabel::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
459{
460 // font + GC
461 option = GetName()+5; // unique digit id of the name
463 parFont.Form("%s::GetDefaultFontStruct()",IsA()->GetName());
464 parGC.Form("%s::GetDefaultGC()()",IsA()->GetName());
465
466 if ((GetDefaultFontStruct() != fFont->GetFontStruct()) || (GetDefaultGC()() != fNormGC)) {
467 TGFont *ufont = fClient->GetResourcePool()->GetFontPool()->FindFont(fFont->GetFontStruct());
468 if (ufont) {
469 ufont->SavePrimitive(out, option);
470 parFont.Form("ufont->GetFontStruct()");
471 }
472
473 TGGC *userGC = fClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
474 if (userGC) {
475 userGC->SavePrimitive(out, option);
476 parGC.Form("uGC->GetGC()");
477 }
478 }
479
480 // save options and color if necessary
481 auto extra_args = SaveCtorArgs(out);
482
483 TString label = GetText()->GetString();
484
485 out << " TGLabel *" << GetName() << " = new TGLabel("<< fParent->GetName()
486 << ", \"" << label.ReplaceSpecialCppChars() << "\"";
487 if ((extra_args.Length() > 0) || (fFont->GetFontStruct() != GetDefaultFontStruct()))
488 out << "," << parGC << "," << parFont << extra_args;
489 else if (fNormGC != GetDefaultGC()())
490 out << ", " << parGC;
491 out << ");\n";
492
493 if (option && strstr(option, "keep_names"))
494 out << " " << GetName() << "->SetName(\"" << GetName() << "\");\n";
495
496 if (fDisabled)
497 out << " " << GetName() << "->Disable();\n";
498
499 out << " " << GetName() << "->SetTextJustify(" << GetTextJustify() << ");\n";
500 out << " " << GetName() << "->SetMargins(" << fMLeft << "," << fMRight << "," << fMTop << "," << fMBottom
501 << ");\n";
502 out << " " << GetName() << "->SetWrapLength(" << fWrapLength << ");\n";
503}
504
505////////////////////////////////////////////////////////////////////////////////
506/// Static returning label default font struct.
507
509{
510 if (!fgDefaultFont) {
511 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
512 }
513 return fgDefaultFont->GetFontStruct();
514}
515
516////////////////////////////////////////////////////////////////////////////////
517/// Static returning label default graphics context.
518
520{
521 if (!fgDefaultGC) {
522 fgDefaultGC = gClient->GetResourcePool()->GetFrameGC();
523 }
524 return *fgDefaultGC;
525}
const Mask_t kWABitGravity
Definition GuiTypes.h:144
Handle_t FontH_t
Font handle (as opposed to Font_t which is an index)
Definition GuiTypes.h:35
const Mask_t kWAWinGravity
Definition GuiTypes.h:145
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:38
@ kRaisedFrame
Definition GuiTypes.h:384
@ kSunkenFrame
Definition GuiTypes.h:383
@ kFixedWidth
Definition GuiTypes.h:387
@ kFixedHeight
Definition GuiTypes.h:389
ULong_t Pixel_t
Pixel value.
Definition GuiTypes.h:40
Handle_t FontStruct_t
Pointer to font structure.
Definition GuiTypes.h:39
#define h(i)
Definition RSha256.hxx:106
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gClient
Definition TGClient.h:157
@ kTextCenterX
Definition TGWidget.h:25
@ kTextLeft
Definition TGWidget.h:23
@ kTextBottom
Definition TGWidget.h:27
@ kTextTop
Definition TGWidget.h:26
@ kTextRight
Definition TGWidget.h:24
@ kTextCenterY
Definition TGWidget.h:28
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char forecolor
Option_t Option_t SetTextFont
Option_t Option_t TPoint TPoint const char mode
Option_t Option_t TPoint TPoint DrawText
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
Option_t Option_t TPoint TPoint const char text
#define hi
#define gVirtualX
Definition TVirtualX.h:337
The color creation and management class.
Definition TColor.h:22
ULong_t GetPixel() const
Return pixel value corresponding to this color.
Definition TColor.cxx:1564
TGFont * GetFont(const char *font, Bool_t fixedDefault=kTRUE)
Get a font from the font pool.
Definition TGClient.cxx:356
const TGResourcePool * GetResourcePool() const
Definition TGClient.h:124
void FreeFont(const TGFont *font)
Free a font.
Definition TGClient.cxx:372
TGGCPool * GetGCPool() const
Definition TGClient.h:132
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition TGClient.cxx:380
TGFontPool * GetFontPool() const
Definition TGClient.h:137
TGFont * GetFont(const char *font, Bool_t fixedDefault=kTRUE)
Get the specified font.
Definition TGFont.cxx:1555
void FreeFont(const TGFont *font)
Free font. If ref count is 0 delete font.
Definition TGFont.cxx:1715
Encapsulate fonts used in the GUI system.
Definition TGFont.h:140
FontStruct_t GetFontStruct() const
Definition TGFont.h:184
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:658
FontH_t GetFontHandle() const
Definition TGFont.h:183
A subclasses of TGWindow, and is used as base class for some simple widgets (buttons,...
Definition TGFrame.h:80
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:597
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition TGFrame.cxx:701
UInt_t fHeight
frame height
Definition TGFrame.h:88
void DoRedraw() override
Redraw the frame.
Definition TGFrame.cxx:422
static const TGGC & GetHilightGC()
Get highlight color graphics context.
Definition TGFrame.cxx:747
virtual UInt_t GetOptions() const
Definition TGFrame.h:199
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition TGFrame.cxx:757
UInt_t fWidth
frame width
Definition TGFrame.h:87
TString SaveCtorArgs(std::ostream &out, UInt_t dflt_options=kChildFrame, Bool_t check_white_pixel=kFALSE)
Return options and custom color as constructor args Used in the SavePrimitive methods,...
Definition TGFrame.cxx:2493
Encapsulate a graphics context used in the low level graphics.
Definition TGGC.h:22
Int_t fMLeft
margin left
Definition TGLabel.h:30
TClass * IsA() const override
Definition TGLabel.h:115
void SetTextJustify(Int_t tmode)
Set text justification.
Definition TGLabel.cxx:395
void Layout() override
Layout label.
Definition TGLabel.cxx:155
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition TGLabel.cxx:361
Int_t f3DStyle
3D style (0 - normal, kRaisedFrame - raised, kSunkenFrame - sunken)
Definition TGLabel.h:35
TGLabel(const TGLabel &)=delete
Bool_t HasOwnFont() const
Returns kTRUE if text attributes are unique.
Definition TGLabel.cxx:450
virtual void DrawText(GContext_t gc, Int_t x, Int_t y)
Draw text at position (x, y).
Definition TGLabel.cxx:191
Bool_t fDisabled
if kTRUE label looks disabled (shaded text)
Definition TGLabel.h:43
~TGLabel() override
Delete label.
Definition TGLabel.cxx:133
static const TGGC * fgDefaultGC
Definition TGLabel.h:49
GContext_t fNormGC
graphics context used for drawing label
Definition TGLabel.h:39
Int_t GetTextJustify() const
Definition TGLabel.h:82
void DoRedraw() override
Redraw label widget.
Definition TGLabel.cxx:200
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save a label widget as a C++ statement(s) on output stream out.
Definition TGLabel.cxx:458
virtual void SetText(TGString *newText)
Set new text in label.
Definition TGLabel.cxx:179
static const TGGC & GetDefaultGC()
Static returning label default graphics context.
Definition TGLabel.cxx:519
Bool_t fHasOwnFont
kTRUE - font defined locally, kFALSE - globally
Definition TGLabel.h:42
UInt_t fTWidth
text width
Definition TGLabel.h:28
Int_t fMRight
margin right
Definition TGLabel.h:31
TGFont * fFont
font to draw label
Definition TGLabel.h:40
UInt_t fTHeight
text height
Definition TGLabel.h:29
Int_t fWrapLength
wrap length
Definition TGLabel.h:36
static FontStruct_t GetDefaultFontStruct()
Static returning label default font struct.
Definition TGLabel.cxx:508
Int_t fMTop
margin top
Definition TGLabel.h:32
TGDimension GetDefaultSize() const override
Return default size.
Definition TGLabel.cxx:167
Int_t fTMode
text alignment
Definition TGLabel.h:34
Bool_t fTextChanged
has text changed
Definition TGLabel.h:38
static const TGFont * fgDefaultFont
Definition TGLabel.h:48
Int_t fMBottom
margin bottom
Definition TGLabel.h:33
virtual void SetTextFont(TGFont *font, Bool_t global=kFALSE)
Changes text font specified by pointer to TGFont object.
Definition TGLabel.cxx:323
Int_t fTFlags
text flags (see TGFont.h ETextLayoutFlags)
Definition TGLabel.h:37
TGTextLayout * fTLayout
text layout
Definition TGLabel.h:41
const TGString * GetText() const
Definition TGLabel.h:74
TGString * fText
label text
Definition TGLabel.h:27
TGClient * fClient
Connection to display server.
Definition TGObject.h:25
Handle_t fId
X11/Win32 Window identifier.
Definition TGObject.h:24
TGString wraps a TString and adds some graphics routines like drawing, size of string on screen depen...
Definition TGString.h:20
Int_t GetLength() const
Definition TGString.h:29
const char * GetString() const
Definition TGString.h:30
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:920
ROOT GUI Window base class.
Definition TGWindow.h:23
const TGWindow * fParent
Parent window.
Definition TGWindow.h:28
virtual void SetWindowName(const char *name=nullptr)
Set window name.
Definition TGWindow.cxx:127
const char * GetName() const override
Return unique name, used in SavePrimitive methods.
Definition TGWindow.cxx:334
Basic string class.
Definition TString.h:138
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Graphics context structure.
Definition GuiTypes.h:224
Attributes that can be used when creating or changing a window.
Definition GuiTypes.h:93