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