TGTableCell.cxx

Go to the documentation of this file.
00001 // Author: Roel Aaij 21/07/2007
00002 
00003 /*************************************************************************
00004  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 #include "TGFrame.h"
00012 #include "TClass.h"
00013 #include "TGWidget.h"
00014 #include "TGWindow.h"
00015 #include "TGResourcePool.h"
00016 #include "Riostream.h"
00017 #include "TSystem.h"
00018 #include "TImage.h"
00019 #include "TEnv.h"
00020 #include "TGToolTip.h"
00021 #include "TGPicture.h"
00022 #include "TGTable.h"
00023 #include "TVirtualTableInterface.h"
00024 #include "TColor.h"
00025 
00026 ClassImp(TGTableCell)
00027 
00028 //////////////////////////////////////////////////////////////////////////
00029 //                                                                      //
00030 // TGTableCell                                                          //
00031 //                                                                      //
00032 // TGTableCell is the class that represents a single cell in a TGTable. //
00033 //                                                                      //
00034 // This class is for internal use in TGTable only.                      //
00035 //                                                                      //
00036 //////////////////////////////////////////////////////////////////////////
00037 
00038 const TGGC *TGTableCell::fgDefaultGC = 0;
00039 const TGFont *TGTableCell::fgDefaultFont = 0;
00040 
00041 //______________________________________________________________________________
00042 TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, TGString *label,
00043                          UInt_t row, UInt_t column, UInt_t width, UInt_t height, 
00044                          GContext_t norm, FontStruct_t font, UInt_t option, 
00045                          Bool_t resize) 
00046    : TGFrame(p, width, height, option), fTip(0), fReadOnly(kFALSE), 
00047      fEnabled(kTRUE), fTMode(kTextRight | kTextCenterY), fImage(0), 
00048      fFontStruct(font), fHasOwnFont(kFALSE), fColumn(column), fRow(row), 
00049      fTable(table)
00050 {
00051    // TGTableCell constructor.
00052    
00053    if (label) {
00054       fLabel = label;
00055    } else {
00056       fLabel = new TGString("0");
00057    }
00058    
00059    fNormGC = norm;
00060    Init(resize);
00061 }
00062 
00063 //______________________________________________________________________________
00064 TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, const char *label, 
00065                          UInt_t row, UInt_t column, UInt_t width, UInt_t height,
00066                          GContext_t norm, FontStruct_t font, UInt_t option, 
00067                          Bool_t resize) 
00068    : TGFrame(p, width, height, option), fTip(0), fReadOnly(kFALSE), 
00069      fEnabled(kTRUE), fTMode(kTextRight | kTextCenterY), fImage(0), 
00070      fFontStruct(font), fHasOwnFont(kFALSE), fColumn(column), fRow(row), 
00071      fTable(table)
00072 {
00073    // TGTableCell constructor
00074    
00075    if (label) {
00076       fLabel = new TGString(label);
00077    } else {
00078       fLabel = new TGString("0");
00079    }
00080 
00081    fNormGC = norm;
00082    Init(resize);
00083 }
00084 
00085 // //______________________________________________________________________________
00086 // TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, TGPicture *image, 
00087 //                          TGString *label, UInt_t row, UInt_t column, 
00088 //                          GContext_t norm, FontStruct_t font, UInt_t option, 
00089 //                          Bool_t resize)
00090 //    : TGFrame(p, 80, 25, option), fTip(0), fReadOnly(kFALSE), fEnabled(kTRUE), 
00091 //      fTMode(kTextRight | kTextCenterY), fImage(image), fFontStruct(font), 
00092 //      fHasOwnFont(kFALSE), fColumn(column), fRow(row), fTable(table)
00093 // {
00094 //    if (label) {
00095 //       fLabel = label;
00096 //    } else {
00097 //       fLabel = new TGString("0");
00098 //    }
00099 //    fNormGC = norm;
00100 
00101 //    Init(resize);
00102 // }
00103 
00104 // //______________________________________________________________________________
00105 // TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, TGPicture *image, 
00106 //                          const char *label, UInt_t row, UInt_t column, 
00107 //                          GContext_t norm, FontStruct_t font, UInt_t option,
00108 //                          Bool_t resize) 
00109 //    : TGFrame(p, 80, 25, option), fTip(0), fReadOnly(kFALSE), fEnabled(kTRUE), 
00110 //      fTMode(kTextRight | kTextCenterY), fImage(image), fFontStruct(font), 
00111 //      fHasOwnFont(kFALSE), fColumn(column), fRow(row), fTable(table)
00112 // {
00113    
00114 //    if (label) {
00115 //       fLabel = new TGString(label);
00116 //    } else {
00117 //       fLabel = new TGString("0");
00118 //    }
00119    
00120 //    fNormGC = norm;
00121 
00122 //    Init(resize);
00123 // }
00124 
00125 
00126 //______________________________________________________________________________
00127 TGTableCell::~TGTableCell()
00128 {
00129    // TGTableCell destructor.
00130 
00131    if (fImage) delete fImage;
00132    if (fLabel) delete fLabel;
00133    if (fTip) delete fTip;
00134 }
00135 
00136 //______________________________________________________________________________
00137 void TGTableCell::Init(Bool_t resize)
00138 {
00139    // Initialise the TGTableCell.
00140 
00141    Int_t max_ascent = 0, max_descent = 0;
00142    
00143    fTWidth  = gVirtualX->TextWidth(fFontStruct, fLabel->GetString(), fLabel->GetLength());
00144    gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
00145    fTHeight = max_ascent + max_descent;
00146 
00147    // Modifications for individual cell drawing test, original block is marked
00148    
00149    if (fTable) {
00150       // Original from here
00151       TGTableHeader *chdr = 0;
00152       TGTableHeader *rhdr = 0;
00153       if(resize) {
00154          chdr = fTable->GetColumnHeader(fColumn);
00155          rhdr = fTable->GetRowHeader(fRow);
00156          SetBackgroundColor(rhdr->GetBackground());
00157          Resize(chdr->GetWidth(), rhdr->GetHeight());
00158       }
00159       SetBackgroundColor(fTable->GetRowBackground(fRow));
00160       // Upto here
00161    } else {
00162       fWidth = 80;
00163       fHeight = 25;
00164       Resize(fWidth, fHeight);
00165       SetBackgroundColor(fgWhitePixel);
00166    }
00167    // End of modifications
00168 
00169 }
00170 
00171 //______________________________________________________________________________
00172 void TGTableCell::DoRedraw()
00173 {
00174    // Redraw the TGTableCell.
00175 
00176    TGFrame::DoRedraw();
00177 
00178    Int_t x = 0, y = 0;
00179 
00180    // To be done: Add a tooltip with the complete label when it
00181    // doesn't fit in the cell.
00182    if (fTWidth > fWidth - 4) fTMode = kTextLeft;
00183    
00184    if (fTMode & kTextLeft) {
00185       x = 4;
00186    } else if (fTMode & kTextRight) {
00187       x = fWidth - fTWidth - 4;
00188    } else {
00189       x = (fWidth - fTWidth) / 2;
00190    }
00191 
00192    if (fTMode & kTextTop) {
00193       y = 3;
00194    } else if (fTMode & kTextBottom) {
00195       y = fHeight - fTHeight - 3;
00196    } else {
00197       y = (fHeight - fTHeight - 4) / 2;
00198    }
00199    
00200    y += fTHeight;
00201 
00202    fLabel->Draw(fId, fNormGC, x, y);
00203 }
00204 
00205 //______________________________________________________________________________
00206 void TGTableCell::MoveDraw(Int_t x, Int_t y)
00207 {
00208    // Move the TGTableCell and redraw it.
00209 
00210    // Note, this method is unused.
00211 
00212    TGFrame::Move(x, y);
00213    DoRedraw();
00214 }
00215 
00216 //______________________________________________________________________________
00217 void TGTableCell::Resize(UInt_t width, UInt_t height)
00218 {
00219    // Resize the TGTableCell.
00220 
00221    fWidth = width;
00222    fHeight = height;
00223    TGFrame::Resize(width, height);
00224    Layout();
00225 }
00226 
00227 //______________________________________________________________________________
00228 void TGTableCell::Resize(TGDimension newsize)
00229 {
00230    // Resize the TGTableCell.
00231 
00232    Resize(newsize.fWidth, newsize.fHeight);
00233 }
00234 
00235 //______________________________________________________________________________
00236 FontStruct_t TGTableCell::GetDefaultFontStruct()
00237 {
00238    // Return default font structure.
00239 
00240    if (!fgDefaultFont)
00241       fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
00242    return fgDefaultFont->GetFontStruct();
00243 }
00244 
00245 //______________________________________________________________________________
00246 const TGGC &TGTableCell::GetDefaultGC()
00247 {
00248    // Return default graphics context.
00249 
00250    if (!fgDefaultGC)
00251       fgDefaultGC = gClient->GetResourcePool()->GetFrameGC();
00252    return *fgDefaultGC;
00253 }
00254 
00255 //______________________________________________________________________________
00256 void TGTableCell::DrawBorder()
00257 {
00258    // Draw the cell border.
00259 
00260    // FIXME Borders are drawn very crudely. There is much room for improvement.
00261    gVirtualX->DrawRectangle(fId, fNormGC, 0, 0, fWidth - 1, fHeight - 1);
00262 }
00263 
00264 //______________________________________________________________________________
00265 void TGTableCell::DrawBorder(Handle_t id, Int_t x, Int_t y)
00266 {
00267    // DrawBorder called from DrawCopy.
00268 
00269    gVirtualX->DrawRectangle(id, fNormGC, x, y, x + fWidth - 1, y +fHeight - 1);
00270 }
00271 
00272 //______________________________________________________________________________
00273 void TGTableCell::Highlight()
00274 {
00275    // Highlight the this TGTableCell.
00276 
00277    // Currently not implemented.
00278 }
00279 
00280 // //______________________________________________________________________________
00281 // void TGTableCell::SetRow(UInt_t row) 
00282 // {
00283 //    fRow = row;
00284 // }
00285 
00286 // //______________________________________________________________________________
00287 // void TGTableCell::SetColumn(UInt_t column)
00288 // {
00289 //    fColumn = column;
00290 // }
00291 
00292 //______________________________________________________________________________
00293 void TGTableCell::DrawCopy(Handle_t id, Int_t x, Int_t y)
00294 {
00295    // Draw list view item in other window.
00296    // List view item is placed and layout in the container frame,
00297    // but is drawn in viewport.
00298    
00299    // FIXME this method is only needed if the table container frame is a 
00300    // TGContainer. It is broken and not used in the current implementation.
00301 
00302    Int_t lx = 0, ly = 0;
00303 
00304    if (fTMode & kTextLeft) {
00305       lx = 4;
00306    } else if (fTMode & kTextRight) {
00307       lx = fWidth - fTWidth - 4;
00308    } else {
00309       lx = (fWidth - fTWidth) / 2;
00310    }
00311 
00312    if (fTMode & kTextTop) {
00313       ly = 3;
00314    } else if (fTMode & kTextBottom) {
00315       ly = fHeight - fTHeight - 3;
00316    } else {
00317       ly = (fHeight - fTHeight - 4) / 2;
00318    }
00319    
00320    ly += fTHeight;
00321    
00322    //    if (fActive) {
00323    //       gVirtualX->SetForeground(fNormGC, fgDefaultSelectedBackground);
00324    //       gVirtualX->FillRectangle(id, fNormGC, x + lx, y + ly, fTWidth, fTHeight + 1);
00325    //       gVirtualX->SetForeground(fNormGC, fClient->GetResourcePool()->GetSelectedFgndColor());
00326    //    } else {
00327 
00328    gVirtualX->SetForeground(fNormGC, fgWhitePixel);
00329    gVirtualX->FillRectangle(id, fNormGC, x, y, fWidth, fHeight);
00330    gVirtualX->SetForeground(fNormGC, fgBlackPixel);
00331    DrawBorder(id, x, y);
00332 
00333    //    }
00334    
00335    fLabel->Draw(id, fNormGC, x + lx, y + ly);
00336 }
00337 
00338 //______________________________________________________________________________
00339 void TGTableCell::SetLabel(const char *label)
00340 {
00341    // Set the label of this cell to label.
00342 
00343    fLabel->SetString(label);
00344 
00345    Int_t max_ascent = 0, max_descent = 0;
00346 
00347    fTWidth  = gVirtualX->TextWidth(fFontStruct, fLabel->GetString(), fLabel->GetLength());
00348    gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
00349    fTHeight = max_ascent + max_descent;
00350 
00351 }
00352 
00353 //______________________________________________________________________________
00354 void TGTableCell::SetImage(TGPicture *image)
00355 {
00356    // Set the image that this cell contains to image.
00357 
00358    // Note: currently not used.
00359    if (fImage) delete fImage;
00360    fImage = image;
00361 }
00362 
00363 // //______________________________________________________________________________
00364 // void TGTableCell::SetBckgndGC(TGGC *gc)
00365 // {
00366 // }
00367 
00368 //______________________________________________________________________________
00369 void TGTableCell::SetFont(FontStruct_t font)
00370 {
00371    // Changes text font.
00372    // If global is kTRUE font is changed globally, otherwise - locally.
00373 
00374    if (font != fFontStruct) {
00375       FontH_t v = gVirtualX->GetFontHandle(font);
00376       if (!v) return;
00377 
00378       fFontStruct = font;
00379       TGGCPool *pool =  fClient->GetResourcePool()->GetGCPool();
00380       TGGC *gc = pool->FindGC(fNormGC);
00381 
00382       gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy
00383       fHasOwnFont = kTRUE;
00384 
00385       gc->SetFont(v);
00386 
00387       fNormGC = gc->GetGC();
00388       gClient->NeedRedraw(this);
00389    }
00390 }
00391 
00392 //______________________________________________________________________________
00393 void TGTableCell::SetFont(const char *fontName)
00394 {
00395    // Changes text font specified by name.
00396    // If global is true color is changed globally, otherwise - locally.
00397 
00398    TGFont *font = fClient->GetFont(fontName);
00399    if (font) {
00400       SetFont(font->GetFontStruct());
00401    }
00402 }
00403 
00404 //______________________________________________________________________________
00405 void TGTableCell::SetTextJustify(Int_t tmode)
00406 {
00407    // Set the text justify mode of the cell to mode.
00408 
00409    fTMode = tmode;
00410 }
00411 
00412 //______________________________________________________________________________
00413 void TGTableCell::Select()
00414 {
00415    // Select this TGTableCell.
00416 
00417    // Note: currently not implemented.
00418 }
00419 
00420 //______________________________________________________________________________
00421 void TGTableCell::SelectRow()
00422 {
00423    // Select the row that this TGTableCell belongs to.
00424 
00425    // Note: currently not implemented.
00426 }
00427 
00428 //______________________________________________________________________________
00429 void TGTableCell::SelectColumn()
00430 {
00431    // Select the column that this TGTableCell belongs to.
00432 
00433    // Note: currently not implemented.
00434 }

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