00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "TGString.h"
00034 #include "TVirtualX.h"
00035 #include "ctype.h"
00036
00037
00038 ClassImp(TGString)
00039 ClassImp(TGHotString)
00040
00041
00042 TGString::TGString(const TGString *s) : TString(s->Data())
00043 {
00044
00045 }
00046
00047
00048 void TGString::Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
00049 {
00050
00051
00052 gVirtualX->DrawString(id, gc, x, y, Data(), Length());
00053 }
00054
00055
00056 void TGString::DrawWrapped(Drawable_t id, GContext_t gc,
00057 Int_t x, Int_t y, UInt_t w, FontStruct_t font)
00058 {
00059
00060
00061
00062 const char *p = Data();
00063 const char *prev = p;
00064 const char *chunk = p;
00065 int tw, th, len = Length();
00066
00067 tw = gVirtualX->TextWidth(font, p, len);
00068 if (tw <= (int)w) {
00069 gVirtualX->DrawString(id, gc, x, y, p, len);
00070 return;
00071 }
00072
00073 int max_ascent, max_descent;
00074 gVirtualX->GetFontProperties(font, max_ascent, max_descent);
00075 th = max_ascent + max_descent + 1;
00076
00077 while(1) {
00078 p = strchr(p, ' ');
00079 if (p == 0) {
00080 if (chunk) gVirtualX->DrawString(id, gc, x, y, chunk, strlen(chunk));
00081 break;
00082 }
00083 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
00084 if (tw > (int)w) {
00085 if (prev == chunk)
00086 prev = ++p;
00087 else
00088 p = prev;
00089 gVirtualX->DrawString(id, gc, x, y, chunk, prev-chunk-1);
00090 chunk = prev;
00091 y += th;
00092 } else {
00093 prev = ++p;
00094 }
00095 }
00096 }
00097
00098
00099 Int_t TGString::GetLines(FontStruct_t font, UInt_t w)
00100 {
00101
00102
00103 const char *p = Data();
00104 const char *prev = p;
00105 const char *chunk = p;
00106 int tw, nlines, len = Length();
00107
00108 nlines = 1;
00109
00110 tw = gVirtualX->TextWidth(font, p, len);
00111 if (tw <= (int)w) return nlines;
00112
00113 while(1) {
00114 p = strchr(p, ' ');
00115 if (p == 0) break;
00116 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
00117 if (tw > (int)w) {
00118 if (prev == chunk)
00119 chunk = prev = ++p;
00120 else
00121 p = chunk = prev;
00122 ++nlines;
00123 } else {
00124 prev = ++p;
00125 }
00126 }
00127 return nlines;
00128 }
00129
00130
00131
00132 TGHotString::TGHotString(const char *s) : TGString()
00133 {
00134
00135
00136 fLastGC = 0;
00137 fOff1 = fOff2 = 0;
00138
00139 fHotChar = 0;
00140 fHotPos = 0;
00141
00142 if (!s) return;
00143
00144 char *dup = StrDup(s);
00145 char *p;
00146
00147 for (p = dup; *p; p++) {
00148 if (*p == '&') {
00149 if (p[1] == '&') {
00150
00151 for (char *tmp = p; *tmp; tmp++)
00152 tmp[0] = tmp[1];
00153 continue;
00154 }
00155
00156 fHotPos = (p - dup) + 1;
00157 fHotChar = tolower(p[1]);
00158 for (; *p; p++) p[0] = p[1];
00159 break;
00160 }
00161 }
00162 Append(dup);
00163 delete [] dup;
00164 }
00165
00166
00167 void TGHotString::Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
00168 {
00169
00170
00171 gVirtualX->DrawString(id, gc, x, y, Data(), Length());
00172
00173 DrawHotChar(id, gc, x, y);
00174 }
00175
00176
00177 void TGHotString::DrawWrapped(Drawable_t id, GContext_t gc,
00178 Int_t x, Int_t y, UInt_t w, FontStruct_t font)
00179 {
00180
00181
00182
00183 const char *p = Data();
00184 const char *prev = p;
00185 const char *chunk = p;
00186 int tw, th, len = Length();
00187
00188 tw = gVirtualX->TextWidth(font, p, len);
00189 if (tw <= (int)w) {
00190 gVirtualX->DrawString(id, gc, x, y, p, len);
00191 DrawHotChar(id, gc, x, y);
00192 return;
00193 }
00194
00195 int max_ascent, max_descent;
00196 gVirtualX->GetFontProperties(font, max_ascent, max_descent);
00197 th = max_ascent + max_descent + 1;
00198
00199 int pcnt = 0;
00200 while(1) {
00201 p = strchr(p, ' ');
00202 if (p == 0) {
00203 if (chunk) {
00204 gVirtualX->DrawString(id, gc, x, y, chunk, strlen(chunk));
00205 if (fHotPos > pcnt && fHotPos <= pcnt+(int)strlen(chunk))
00206 DrawHotChar(id, gc, x, y);
00207 }
00208 break;
00209 }
00210 tw = gVirtualX->TextWidth(font, chunk, p-chunk);
00211 if (tw > (int)w) {
00212 if (prev == chunk)
00213 prev = ++p;
00214 else
00215 p = prev;
00216 gVirtualX->DrawString(id, gc, x, y, chunk, prev-chunk-1);
00217 if (fHotPos > pcnt && fHotPos <= pcnt+prev-chunk-1)
00218 DrawHotChar(id, gc, x, y);
00219 pcnt = prev-chunk-1;
00220 chunk = prev;
00221 y += th;
00222 } else {
00223 prev = ++p;
00224 }
00225 }
00226 }
00227
00228
00229 void TGHotString::DrawHotChar(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
00230 {
00231
00232
00233 if (fHotPos > 0) {
00234 if (fLastGC != gc) {
00235 GCValues_t gcval;
00236 FontStruct_t font;
00237
00238 gcval.fMask = kGCFont;
00239 gVirtualX->GetGCValues(gc, gcval);
00240 font = gVirtualX->GetFontStruct(gcval.fFont);
00241
00242 fOff1 = gVirtualX->TextWidth(font, Data(), fHotPos-1);
00243 fOff2 = gVirtualX->TextWidth(font, Data(), fHotPos) - 1;
00244
00245 gVirtualX->FreeFontStruct(font);
00246 fLastGC = gc;
00247 }
00248 gVirtualX->DrawLine(id, gc, x+fOff1, y+1, x+fOff2, y+1);
00249 }
00250 }
00251