Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGString.cxx
Go to the documentation of this file.
1// @(#)root/gui:$Id$
2// Author: Fons Rademakers 05/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 TGString
25 \ingroup guiwidgets
26
27TGString wraps a TString and adds some graphics routines like
28drawing, size of string on screen depending on font, etc.
29
30\class TGHotString
31\ingroup guiwidgets
32
33TGHotString is a string with a "hot" character underlined.
34
35*/
36
37
38#include "TGString.h"
39#include "TVirtualX.h"
40#include "ctype.h"
41
42
43
44////////////////////////////////////////////////////////////////////////////////
45/// constructor
46
47TGString::TGString(const TGString *s) : TString(s->Data())
48{
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Draw string.
53
55{
56 gVirtualX->DrawString(id, gc, x, y, Data(), Length());
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// Draw a string in a column with width w. If string is longer than
61/// w wrap it to next line.
62
65{
66 const char *p = Data();
67 const char *prev = p;
68 const char *chunk = p;
69 int tw, th, len = Length();
70
71 tw = gVirtualX->TextWidth(font, p, len);
72 if (tw <= (int)w) {
73 gVirtualX->DrawString(id, gc, x, y, p, len);
74 return;
75 }
76
78 gVirtualX->GetFontProperties(font, max_ascent, max_descent);
79 th = max_ascent + max_descent + 1;
80
81 while(1) {
82 p = strchr(p, ' ');
83 if (p == 0) {
84 if (chunk) gVirtualX->DrawString(id, gc, x, y, chunk, strlen(chunk));
85 break;
86 }
87 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
88 if (tw > (int)w) {
89 if (prev == chunk)
90 prev = ++p;
91 else
92 p = prev;
93 gVirtualX->DrawString(id, gc, x, y, chunk, prev-chunk-1);
94 chunk = prev;
95 y += th;
96 } else {
97 prev = ++p;
98 }
99 }
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Get number of lines of width w the string would take using a certain font.
104
106{
107 const char *p = Data();
108 const char *prev = p;
109 const char *chunk = p;
110 int tw, nlines, len = Length();
111
112 nlines = 1;
113
114 tw = gVirtualX->TextWidth(font, p, len);
115 if (tw <= (int)w) return nlines;
116
117 while(1) {
118 p = strchr(p, ' ');
119 if (p == 0) break;
120 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
121 if (tw > (int)w) {
122 if (prev == chunk)
123 chunk = prev = ++p;
124 else
125 p = chunk = prev;
126 ++nlines;
127 } else {
128 prev = ++p;
129 }
130 }
131 return nlines;
132}
133
134
135////////////////////////////////////////////////////////////////////////////////
136/// Create a hot string.
137
139{
140 fLastGC = 0;
141 fOff1 = fOff2 = 0;
142
143 fHotChar = 0;
144 fHotPos = 0; // No hotkey defaults the offset to zero
145
146 if (!s) return;
147
148 char *dup = StrDup(s);
149 char *p;
150
151 for (p = dup; *p; p++) {
152 if (*p == '&') {
153 if (p[1] == '&') { // escaped & ?
154 // copy the string down over it
155 for (char *tmp = p; *tmp; tmp++)
156 tmp[0] = tmp[1];
157 continue; // and skip to the key char
158 }
159 // hot key marker - calculate the offset value
160 fHotPos = (p - dup) + 1;
161 fHotChar = tolower(p[1]);
162 for (; *p; p++) p[0] = p[1]; // copy down
163 break; // allow only one hotkey per item
164 }
165 }
166 Append(dup);
167 delete [] dup;
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Draw a hot string and underline the hot character.
172
174{
175 gVirtualX->DrawString(id, gc, x, y, Data(), Length());
176
177 DrawHotChar(id, gc, x, y);
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Draw a hot string in a column with width w. If string is longer than
182/// w wrap it to next line.
183
186{
187 const char *p = Data();
188 const char *prev = p;
189 const char *chunk = p;
190 int tw, th, len = Length();
191
192 tw = gVirtualX->TextWidth(font, p, len);
193 if (tw <= (int)w) {
194 gVirtualX->DrawString(id, gc, x, y, p, len);
195 DrawHotChar(id, gc, x, y);
196 return;
197 }
198
200 gVirtualX->GetFontProperties(font, max_ascent, max_descent);
201 th = max_ascent + max_descent + 1;
202
203 int pcnt = 0;
204 while(1) {
205 p = strchr(p, ' ');
206 if (p == 0) {
207 if (chunk) {
208 gVirtualX->DrawString(id, gc, x, y, chunk, strlen(chunk));
209 if (fHotPos > pcnt && fHotPos <= pcnt+(int)strlen(chunk))
210 DrawHotChar(id, gc, x, y);
211 }
212 break;
213 }
214 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
215 if (tw > (int)w) {
216 if (prev == chunk)
217 prev = ++p;
218 else
219 p = prev;
220 gVirtualX->DrawString(id, gc, x, y, chunk, prev-chunk-1);
221 if (fHotPos > pcnt && fHotPos <= pcnt+prev-chunk-1)
222 DrawHotChar(id, gc, x, y);
223 pcnt = prev-chunk-1;
224 chunk = prev;
225 y += th;
226 } else {
227 prev = ++p;
228 }
229 }
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// Draw the underline under the hot character.
234
236{
237 if (fHotPos > 0) {
238 if (fLastGC != gc) {
240 FontStruct_t font;
241 font = gVirtualX->GetGCFont(gc);
242 if (font) {
243 fOff1 = gVirtualX->TextWidth(font, Data(), fHotPos-1); //+1;
244 fOff2 = gVirtualX->TextWidth(font, Data(), fHotPos) - 1;
245 }
246 else {
247 gcval.fMask = kGCFont;
248 gVirtualX->GetGCValues(gc, gcval);
249 font = gVirtualX->GetFontStruct(gcval.fFont);
250
251 fOff1 = gVirtualX->TextWidth(font, Data(), fHotPos-1); //+1;
252 fOff2 = gVirtualX->TextWidth(font, Data(), fHotPos) - 1;
253
254 gVirtualX->FreeFontStruct(font);
255 }
256 fLastGC = gc;
257 }
258 gVirtualX->DrawLine(id, gc, x+fOff1, y+1, x+fOff2, y+1);
259 }
260}
261
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:38
Handle_t Drawable_t
Drawable handle.
Definition GuiTypes.h:31
const Mask_t kGCFont
Definition GuiTypes.h:300
Handle_t FontStruct_t
Pointer to font structure.
Definition GuiTypes.h:39
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
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 depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
char * StrDup(const char *str)
Duplicate the string str.
Definition TString.cxx:2563
#define gVirtualX
Definition TVirtualX.h:337
TGHotString(const char *s)
Create a hot string.
Definition TGString.cxx:138
void Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y) override
Draw a hot string and underline the hot character.
Definition TGString.cxx:173
Int_t fOff1
variable used during drawing (cache)
Definition TGString.h:49
char fHotChar
hot character
Definition TGString.h:45
void DrawHotChar(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
Draw the underline under the hot character.
Definition TGString.cxx:235
void DrawWrapped(Drawable_t id, GContext_t gc, Int_t x, Int_t y, UInt_t w, FontStruct_t font) override
Draw a hot string in a column with width w.
Definition TGString.cxx:184
GContext_t fLastGC
context used during last drawing
Definition TGString.h:48
Int_t fHotPos
position of hot character
Definition TGString.h:46
Int_t fOff2
variable used during drawing (cache)
Definition TGString.h:50
TGString wraps a TString and adds some graphics routines like drawing, size of string on screen depen...
Definition TGString.h:20
virtual void Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
Draw string.
Definition TGString.cxx:54
virtual Int_t GetLines(FontStruct_t font, UInt_t w)
Get number of lines of width w the string would take using a certain font.
Definition TGString.cxx:105
TGString()
Definition TGString.h:23
virtual void DrawWrapped(Drawable_t id, GContext_t gc, Int_t x, Int_t y, UInt_t w, FontStruct_t font)
Draw a string in a column with width w.
Definition TGString.cxx:63
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
const char * Data() const
Definition TString.h:384
TString & Append(const char *cs)
Definition TString.h:580
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Graphics context structure.
Definition GuiTypes.h:224