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 #include "TGStatusBar.h"
00032 #include "TGResourcePool.h"
00033 #include "TList.h"
00034 #include "Riostream.h"
00035 
00036 
00037 const TGFont  *TGStatusBar::fgDefaultFont = 0;
00038 TGGC          *TGStatusBar::fgDefaultGC = 0;
00039 
00040 
00041 class TGStatusBarPart : public TGHorizontalFrame {
00042 friend class TGStatusBar;
00043 private:
00044    TGString  *fStatusInfo;    
00045    Int_t      fYt;            
00046    virtual void DoRedraw();
00047 
00048 public:
00049    TGStatusBarPart(const TGWindow *p, Int_t h, Int_t y, ULong_t back = GetDefaultFrameBackground());
00050    ~TGStatusBarPart() { delete fStatusInfo; DestroyWindow(); }
00051    void SetText(TGString *text);
00052    const TGString *GetText() const { return fStatusInfo; }
00053 };
00054 
00055 
00056 TGStatusBarPart::TGStatusBarPart(const TGWindow *p, Int_t h, Int_t y, ULong_t back)
00057    : TGHorizontalFrame(p, 5, 5, kChildFrame | kHorizontalFrame, back)
00058 {
00059    
00060    
00061 
00062    fStatusInfo = 0;
00063    fYt = y + 1;
00064    fHeight = h;
00065    MapWindow();
00066 
00067    fEditDisabled = kEditDisableGrab;
00068 }
00069 
00070 
00071 void TGStatusBarPart::SetText(TGString *text)
00072 {
00073    
00074 
00075    if (fStatusInfo) delete fStatusInfo;
00076    fStatusInfo = text;
00077    fClient->NeedRedraw(this);
00078 }
00079 
00080 
00081 void TGStatusBarPart::DoRedraw()
00082 {
00083    
00084 
00085    TGHorizontalFrame::DoRedraw();
00086 
00087    if (fStatusInfo)
00088       fStatusInfo->Draw(fId, TGStatusBar::GetDefaultGC()(), 3, fYt);
00089 }
00090 
00091 
00092 ClassImp(TGStatusBar)
00093 
00094 
00095 TGStatusBar::TGStatusBar(const TGWindow *p, UInt_t w, UInt_t h,
00096                          UInt_t options, ULong_t back) :
00097    TGHorizontalFrame(p, w, h, options, back)
00098 {
00099    
00100    
00101 
00102    fBorderWidth   = 2;
00103    fStatusPart    = new TGStatusBarPart* [1];
00104    fParts         = new Int_t [1];
00105    fXt            = new Int_t [1];
00106    fParts[0]      = 100;
00107    fNpart         = 1;
00108    f3DCorner      = kTRUE;
00109 
00110    int max_ascent, max_descent;
00111    gVirtualX->GetFontProperties(GetDefaultFontStruct(), max_ascent, max_descent);
00112    int ht = max_ascent + max_descent;
00113 
00114    fYt = max_ascent;
00115 
00116    fStatusPart[0] = new TGStatusBarPart(this, ht, fYt);
00117    AddFrame(fStatusPart[0]);
00118    Resize(w, ht + 5);
00119 
00120    
00121 }
00122 
00123 
00124 TGStatusBar::~TGStatusBar()
00125 {
00126    
00127 
00128    if (!MustCleanup()) {
00129       for (int i = 0; i < fNpart; i++) {
00130          delete fStatusPart[i];
00131       }
00132    }
00133 
00134    delete [] fStatusPart;
00135    delete [] fParts;
00136    delete [] fXt;
00137 }
00138 
00139 
00140 void TGStatusBar::SetText(TGString *text, Int_t partidx)
00141 {
00142    
00143    
00144 
00145    if (partidx < 0 || partidx >= fNpart) {
00146       Error("SetText", "partidx out of range (0,%d)", fNpart-1);
00147       return;
00148    }
00149 
00150    fStatusPart[partidx]->SetText(text);
00151 }
00152 
00153 
00154 void TGStatusBar::SetText(const char *text, Int_t partidx)
00155 {
00156    
00157 
00158    SetText(new TGString(text), partidx);
00159 }
00160 
00161 
00162 const char *TGStatusBar::GetText(Int_t partidx) const
00163 {
00164    
00165 
00166    if (partidx < 0 || partidx >= fNpart) {
00167       Error("GetText", "partidx out of range (0,%d)", fNpart-1);
00168       return 0;
00169    }
00170 
00171    const TGString *str = fStatusPart[partidx]->GetText();
00172    return str->Data();
00173 }
00174 
00175 
00176 void TGStatusBar::DrawBorder()
00177 {
00178    
00179 
00180    
00181    int i;
00182    for (i = 0; i < fNpart; i++) {
00183       if (i == 0)
00184          fXt[i] = 0;
00185       else
00186          fXt[i] = fXt[i-1] + (fWidth * fParts[i-1] / 100);
00187    }
00188 
00189    
00190    for (i = 0; i < fNpart; i++) {
00191       int xmax, xmin = fXt[i];
00192       if (i == fNpart-1)
00193          xmax = fWidth;
00194       else
00195          xmax = fXt[i+1] - 2;
00196 
00197       if (i == fNpart-1) {
00198          if (f3DCorner)
00199             fStatusPart[i]->MoveResize(fXt[i]+2, 1, xmax - fXt[i] - 15, fHeight - 2);
00200          else
00201             fStatusPart[i]->MoveResize(fXt[i]+2, 1, xmax - fXt[i], fHeight - 2);
00202       } else
00203          fStatusPart[i]->MoveResize(fXt[i]+2, 1, xmax - fXt[i] - 4, fHeight - 2);
00204 
00205       gVirtualX->DrawLine(fId, GetShadowGC()(), xmin, 0, xmax-2, 0);
00206       gVirtualX->DrawLine(fId, GetShadowGC()(), xmin, 0, xmin, fHeight-2);
00207       gVirtualX->DrawLine(fId, GetHilightGC()(), xmin, fHeight-1, xmax-1, fHeight-1);
00208       if (i == fNpart-1)
00209          gVirtualX->DrawLine(fId, GetHilightGC()(), xmax-1, fHeight-1, xmax-1, 0);
00210       else
00211          gVirtualX->DrawLine(fId, GetHilightGC()(), xmax-1, fHeight-1, xmax-1, 1);
00212    }
00213 
00214    
00215    if (f3DCorner) {
00216       gVirtualX->DrawLine(fId, GetShadowGC()(),  fWidth-3,  fHeight-2, fWidth-2, fHeight-3);
00217       gVirtualX->DrawLine(fId, GetShadowGC()(),  fWidth-4,  fHeight-2, fWidth-2, fHeight-4);
00218       gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-5,  fHeight-2, fWidth-2, fHeight-5);
00219 
00220       gVirtualX->DrawLine(fId, GetShadowGC()(),  fWidth-7,  fHeight-2, fWidth-2, fHeight-7);
00221       gVirtualX->DrawLine(fId, GetShadowGC()(),  fWidth-8,  fHeight-2, fWidth-2, fHeight-8);
00222       gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-9,  fHeight-2, fWidth-2, fHeight-9);
00223 
00224       gVirtualX->DrawLine(fId, GetShadowGC()(),  fWidth-11, fHeight-2, fWidth-2, fHeight-11);
00225       gVirtualX->DrawLine(fId, GetShadowGC()(),  fWidth-12, fHeight-2, fWidth-2, fHeight-12);
00226       gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-13, fHeight-2, fWidth-2, fHeight-13);
00227 
00228       gVirtualX->DrawLine(fId, GetBckgndGC()(),  fWidth-13, fHeight-1, fWidth-1, fHeight-1);
00229       gVirtualX->DrawLine(fId, GetBckgndGC()(),  fWidth-1,  fHeight-1, fWidth-1, fHeight-13);
00230    }
00231 }
00232 
00233 
00234 void TGStatusBar::DoRedraw()
00235 {
00236    
00237 
00238    
00239    TGFrame::DoRedraw();
00240 
00241    for (int i = 0; i < fNpart; i++)
00242       fStatusPart[i]->DoRedraw();
00243 }
00244 
00245 
00246 void TGStatusBar::SetParts(Int_t *parts, Int_t npart)
00247 {
00248    
00249    
00250 
00251    if (npart < 1) {
00252       Warning("SetParts", "must be at least one part");
00253       npart = 1;
00254    }
00255    if (npart > 15) {
00256       Error("SetParts", "to many parts (limit is 15)");
00257       return;
00258    }
00259 
00260    int i;
00261    for (i = 0; i < fNpart; i++)
00262       delete fStatusPart[i];
00263 
00264    delete [] fStatusPart;
00265    delete [] fParts;
00266    delete [] fXt;
00267    fList->Delete();
00268 
00269    fStatusPart = new TGStatusBarPart* [npart];
00270    fParts      = new Int_t [npart];
00271    fXt         = new Int_t [npart];
00272 
00273    int tot = 0;
00274    for (i = 0; i < npart; i++) {
00275       fStatusPart[i] = new TGStatusBarPart(this, fHeight, fYt);
00276       AddFrame(fStatusPart[i]);
00277       fParts[i] = parts[i];
00278       tot += parts[i];
00279       if (tot > 100)
00280          Error("SetParts", "sum of part > 100");
00281    }
00282    if (tot < 100)
00283       fParts[npart-1] += 100 - tot;
00284    fNpart = npart;
00285 }
00286 
00287 
00288 void TGStatusBar::SetParts(Int_t npart)
00289 {
00290    
00291 
00292    if (npart < 1) {
00293       Warning("SetParts", "must be at least one part");
00294       npart = 1;
00295    }
00296    if (npart > 40) {
00297       Error("SetParts", "to many parts (limit is 40)");
00298       return;
00299    }
00300 
00301    int i;
00302    for (i = 0; i < fNpart; i++)
00303       delete fStatusPart[i];
00304 
00305    delete [] fStatusPart;
00306    delete [] fParts;
00307    delete [] fXt;
00308    fList->Delete();
00309 
00310    fStatusPart = new TGStatusBarPart* [npart];
00311    fParts      = new Int_t [npart];
00312    fXt         = new Int_t [npart];
00313 
00314    int sz  = 100/npart;
00315    int tot = 0;
00316    for (i = 0; i < npart; i++) {
00317       fStatusPart[i] = new TGStatusBarPart(this, fHeight, fYt);
00318       AddFrame(fStatusPart[i]);
00319       fParts[i] = sz;
00320       tot += sz;
00321    }
00322    if (tot < 100)
00323       fParts[npart-1] += 100 - tot;
00324    fNpart = npart;
00325 }
00326 
00327 
00328 FontStruct_t TGStatusBar::GetDefaultFontStruct()
00329 {
00330    
00331 
00332    if (!fgDefaultFont)
00333       fgDefaultFont = gClient->GetResourcePool()->GetStatusFont();
00334    return fgDefaultFont->GetFontStruct();
00335 }
00336 
00337 
00338 const TGGC &TGStatusBar::GetDefaultGC()
00339 {
00340    
00341 
00342    if (!fgDefaultGC) {
00343       fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
00344       fgDefaultGC->SetFont(fgDefaultFont->GetFontHandle());
00345    }
00346    return *fgDefaultGC;
00347 }
00348 
00349 
00350 TGCompositeFrame *TGStatusBar::GetBarPart(Int_t npart) const
00351 {
00352    
00353    
00354 
00355    return  ((npart<fNpart) && (npart>=0)) ? (TGCompositeFrame*)fStatusPart[npart] : 0;
00356 }
00357 
00358 
00359 TGDimension TGStatusBar::GetDefaultSize() const
00360 {
00361    
00362 
00363    UInt_t h = fHeight;
00364 
00365    for (int i = 0; i < fNpart; i++) {
00366       h = TMath::Max(h,fStatusPart[i]->GetDefaultHeight()+1);
00367    }
00368    return TGDimension(fWidth, h);
00369 }
00370 
00371 
00372 void TGStatusBar::SavePrimitive(ostream &out, Option_t *option )
00373 {
00374     
00375 
00376    if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
00377 
00378    out << endl;
00379    out << "   // status bar" << endl;
00380 
00381    out << "   TGStatusBar *";
00382    out << GetName() <<" = new TGStatusBar("<< fParent->GetName()
00383        << "," << GetWidth() << "," << GetHeight();
00384 
00385    if (fBackground == GetDefaultFrameBackground()) {
00386       if (GetOptions() == (kSunkenFrame | kHorizontalFrame)) {
00387          out <<");" << endl;
00388       } else {
00389          out << "," << GetOptionString() <<");" << endl;
00390       }
00391    } else {
00392       out << "," << GetOptionString() << ",ucolor);" << endl;
00393    }
00394    if (option && strstr(option, "keep_names"))
00395       out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00396 
00397    int i; char quote = '"';
00398 
00399    if (fNpart > 1)  {
00400       out << "   Int_t parts" << GetName()+5 << "[] = {" << fParts[0];
00401 
00402       for (i=1; i<fNpart; i++) {
00403          out  << "," << fParts[i];
00404       }
00405       out << "};" << endl;
00406 
00407       out << "   " << GetName() << "->SetParts(parts" << GetName()+5
00408           << "," << fNpart << ");" <<endl;
00409    }
00410    for (i=0; i<fNpart; i++) {
00411       if (fStatusPart[i]->GetText()) {
00412          out << "   " << GetName() << "->SetText(" << quote
00413              << fStatusPart[i]->GetText()->GetString()
00414              << quote << "," << i << ");" << endl;
00415       } else {
00416          if (!fStatusPart[i]->GetList()->First()) continue;
00417          out << "   TGCompositeFrame *" << fStatusPart[i]->GetName()
00418              << " = " << GetName() << "->GetBarPart(" << i << ");" << endl;
00419 
00420          TGFrameElement *el;
00421          TIter next(fStatusPart[i]->GetList());
00422 
00423          while ((el = (TGFrameElement *) next())) {
00424             el->fFrame->SavePrimitive(out, option);
00425             out << "   " << fStatusPart[i]->GetName() << "->AddFrame("
00426                 << el->fFrame->GetName();
00427             el->fLayout->SavePrimitive(out, option);
00428             out << ");" << endl;
00429          }
00430       }
00431    }
00432 }