TGString.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGString.cxx 23115 2008-04-10 13:35:37Z rdm $
00002 // Author: Fons Rademakers   05/01/98
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 /**************************************************************************
00012 
00013     This source is based on Xclass95, a Win95-looking GUI toolkit.
00014     Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
00015 
00016     Xclass95 is free software; you can redistribute it and/or
00017     modify it under the terms of the GNU Library General Public
00018     License as published by the Free Software Foundation; either
00019     version 2 of the License, or (at your option) any later version.
00020 
00021 **************************************************************************/
00022 
00023 //////////////////////////////////////////////////////////////////////////
00024 //                                                                      //
00025 // TGString and TGHotString                                             //
00026 //                                                                      //
00027 // TGString wraps a TString and adds some graphics routines like        //
00028 // drawing, size of string on screen depending on font, etc.            //
00029 // TGHotString is a string with a "hot" character unerlined.            //
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    // cconstructor
00045 }
00046 
00047 //______________________________________________________________________________
00048 void TGString::Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
00049 {
00050    // Draw string.
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    // Draw a string in a column with width w. If string is longer than
00060    // w wrap it to next line.
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    // Get number of lines of width w the string would take using a certain font.
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    // Create a hot string.
00135 
00136    fLastGC = 0;
00137    fOff1 = fOff2 = 0;
00138 
00139    fHotChar = 0;
00140    fHotPos  = 0;    // No hotkey defaults the offset to zero
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] == '&') { // escaped & ?
00150             // copy the string down over it
00151             for (char *tmp = p; *tmp; tmp++)
00152                tmp[0] = tmp[1];
00153             continue; // and skip to the key char
00154          }
00155          // hot key marker - calculate the offset value
00156          fHotPos  = (p - dup) + 1;
00157          fHotChar = tolower(p[1]);
00158          for (; *p; p++) p[0] = p[1];  // copy down
00159          break;                        // allow only one hotkey per item
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    // Draw a hot string and underline the hot character.
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    // Draw a hot string in a column with width w. If string is longer than
00181    // w wrap it to next line.
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    // Draw the underline under the hot character.
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); //+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 

Generated on Tue Jul 5 14:22:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1