Logo ROOT  
Reference Guide
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
45
46////////////////////////////////////////////////////////////////////////////////
47/// constructor
48
50{
51}
52
53////////////////////////////////////////////////////////////////////////////////
54/// Draw string.
55
57{
58 gVirtualX->DrawString(id, gc, x, y, Data(), Length());
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// Draw a string in a column with width w. If string is longer than
63/// w wrap it to next line.
64
66 Int_t x, Int_t y, UInt_t w, FontStruct_t font)
67{
68 const char *p = Data();
69 const char *prev = p;
70 const char *chunk = p;
71 int tw, th, len = Length();
72
73 tw = gVirtualX->TextWidth(font, p, len);
74 if (tw <= (int)w) {
75 gVirtualX->DrawString(id, gc, x, y, p, len);
76 return;
77 }
78
79 int max_ascent, max_descent;
80 gVirtualX->GetFontProperties(font, max_ascent, max_descent);
81 th = max_ascent + max_descent + 1;
82
83 while(1) {
84 p = strchr(p, ' ');
85 if (p == 0) {
86 if (chunk) gVirtualX->DrawString(id, gc, x, y, chunk, strlen(chunk));
87 break;
88 }
89 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
90 if (tw > (int)w) {
91 if (prev == chunk)
92 prev = ++p;
93 else
94 p = prev;
95 gVirtualX->DrawString(id, gc, x, y, chunk, prev-chunk-1);
96 chunk = prev;
97 y += th;
98 } else {
99 prev = ++p;
100 }
101 }
102}
103
104////////////////////////////////////////////////////////////////////////////////
105/// Get number of lines of width w the string would take using a certain font.
106
108{
109 const char *p = Data();
110 const char *prev = p;
111 const char *chunk = p;
112 int tw, nlines, len = Length();
113
114 nlines = 1;
115
116 tw = gVirtualX->TextWidth(font, p, len);
117 if (tw <= (int)w) return nlines;
118
119 while(1) {
120 p = strchr(p, ' ');
121 if (p == 0) break;
122 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
123 if (tw > (int)w) {
124 if (prev == chunk)
125 chunk = prev = ++p;
126 else
127 p = chunk = prev;
128 ++nlines;
129 } else {
130 prev = ++p;
131 }
132 }
133 return nlines;
134}
135
136
137////////////////////////////////////////////////////////////////////////////////
138/// Create a hot string.
139
141{
142 fLastGC = 0;
143 fOff1 = fOff2 = 0;
144
145 fHotChar = 0;
146 fHotPos = 0; // No hotkey defaults the offset to zero
147
148 if (!s) return;
149
150 char *dup = StrDup(s);
151 char *p;
152
153 for (p = dup; *p; p++) {
154 if (*p == '&') {
155 if (p[1] == '&') { // escaped & ?
156 // copy the string down over it
157 for (char *tmp = p; *tmp; tmp++)
158 tmp[0] = tmp[1];
159 continue; // and skip to the key char
160 }
161 // hot key marker - calculate the offset value
162 fHotPos = (p - dup) + 1;
163 fHotChar = tolower(p[1]);
164 for (; *p; p++) p[0] = p[1]; // copy down
165 break; // allow only one hotkey per item
166 }
167 }
168 Append(dup);
169 delete [] dup;
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Draw a hot string and underline the hot character.
174
176{
177 gVirtualX->DrawString(id, gc, x, y, Data(), Length());
178
179 DrawHotChar(id, gc, x, y);
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Draw a hot string in a column with width w. If string is longer than
184/// w wrap it to next line.
185
187 Int_t x, Int_t y, UInt_t w, FontStruct_t font)
188{
189 const char *p = Data();
190 const char *prev = p;
191 const char *chunk = p;
192 int tw, th, len = Length();
193
194 tw = gVirtualX->TextWidth(font, p, len);
195 if (tw <= (int)w) {
196 gVirtualX->DrawString(id, gc, x, y, p, len);
197 DrawHotChar(id, gc, x, y);
198 return;
199 }
200
201 int max_ascent, max_descent;
202 gVirtualX->GetFontProperties(font, max_ascent, max_descent);
203 th = max_ascent + max_descent + 1;
204
205 int pcnt = 0;
206 while(1) {
207 p = strchr(p, ' ');
208 if (p == 0) {
209 if (chunk) {
210 gVirtualX->DrawString(id, gc, x, y, chunk, strlen(chunk));
211 if (fHotPos > pcnt && fHotPos <= pcnt+(int)strlen(chunk))
212 DrawHotChar(id, gc, x, y);
213 }
214 break;
215 }
216 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
217 if (tw > (int)w) {
218 if (prev == chunk)
219 prev = ++p;
220 else
221 p = prev;
222 gVirtualX->DrawString(id, gc, x, y, chunk, prev-chunk-1);
223 if (fHotPos > pcnt && fHotPos <= pcnt+prev-chunk-1)
224 DrawHotChar(id, gc, x, y);
225 pcnt = prev-chunk-1;
226 chunk = prev;
227 y += th;
228 } else {
229 prev = ++p;
230 }
231 }
232}
233
234////////////////////////////////////////////////////////////////////////////////
235/// Draw the underline under the hot character.
236
238{
239 if (fHotPos > 0) {
240 if (fLastGC != gc) {
241 GCValues_t gcval;
242 FontStruct_t font;
243 font = gVirtualX->GetGCFont(gc);
244 if (font) {
245 fOff1 = gVirtualX->TextWidth(font, Data(), fHotPos-1); //+1;
246 fOff2 = gVirtualX->TextWidth(font, Data(), fHotPos) - 1;
247 }
248 else {
249 gcval.fMask = kGCFont;
250 gVirtualX->GetGCValues(gc, gcval);
251 font = gVirtualX->GetFontStruct(gcval.fFont);
252
253 fOff1 = gVirtualX->TextWidth(font, Data(), fHotPos-1); //+1;
254 fOff2 = gVirtualX->TextWidth(font, Data(), fHotPos) - 1;
255
256 gVirtualX->FreeFontStruct(font);
257 }
258 fLastGC = gc;
259 }
260 gVirtualX->DrawLine(id, gc, x+fOff1, y+1, x+fOff2, y+1);
261 }
262}
263
Handle_t Drawable_t
Drawable handle.
Definition: GuiTypes.h:31
const Mask_t kGCFont
Definition: GuiTypes.h:300
Handle_t GContext_t
Graphics context handle.
Definition: GuiTypes.h:38
Handle_t FontStruct_t
Pointer to font structure.
Definition: GuiTypes.h:39
int Int_t
Definition: RtypesCore.h:45
unsigned int UInt_t
Definition: RtypesCore.h:46
#define ClassImp(name)
Definition: Rtypes.h:364
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2515
#define gVirtualX
Definition: TVirtualX.h:338
TGHotString is a string with a "hot" character underlined.
Definition: TGString.h:42
TGHotString(const char *s)
Create a hot string.
Definition: TGString.cxx:140
virtual void DrawWrapped(Drawable_t id, GContext_t gc, Int_t x, Int_t y, UInt_t w, FontStruct_t font)
Draw a hot string in a column with width w.
Definition: TGString.cxx:186
Int_t fOff1
variable used during drawing (cache)
Definition: TGString.h:49
virtual void Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
Draw a hot string and underline the hot character.
Definition: TGString.cxx:175
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:237
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:56
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:107
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:65
Basic string class.
Definition: TString.h:136
Ssiz_t Length() const
Definition: TString.h:410
const char * Data() const
Definition: TString.h:369
TString & Append(const char *cs)
Definition: TString.h:564
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
static constexpr double s
Graphics context structure.
Definition: GuiTypes.h:224
Mask_t fMask
bit mask specifying which fields are valid
Definition: GuiTypes.h:251
FontH_t fFont
default text font for text operations
Definition: GuiTypes.h:242