#include "TGCanvas.h"
#include "TGFrame.h"
#include "TClass.h"
#include "TGWindow.h"
#include "TGResourcePool.h"
#include "Riostream.h"
#include "TSystem.h"
#include "TImage.h"
#include "TEnv.h"
#include "TGToolTip.h"
#include "TGWidget.h"
#include "TGPicture.h"
#include "TRandom3.h"
#include "TVirtualTableInterface.h"
#include "TGTable.h"
#include "TGTableCell.h"
#include "TGTableHeader.h"
#include "TObjArray.h"
#include "TGTableContainer.h"
#include "TGScrollBar.h"
#include "TGButton.h"
#include "TGNumberEntry.h"
#include "TGTextEntry.h"
#include "TGLabel.h"
#include "TColor.h"
ClassImp(TGTable)
ClassImp(TTableRange)
TGTable::TGTable(const TGWindow *p, Int_t id, TVirtualTableInterface *interface,
UInt_t nrows, UInt_t ncolumns)
: TGCompositeFrame(p, 500, 500, kVerticalFrame), TGWidget(id), fRows(0),
fRowHeaders(0), fColumnHeaders(0), fReadOnly(kFALSE), fSelectColor(0),
fTMode(0), fAllData(kFALSE), fTableFrame(0), fCanvas(0), fCellWidth(80),
fCellHeight(25), fInterface(interface)
{
fCurrentRange = new TTableRange();
fDataRange = new TTableRange();
fGotoRange = new TTableRange();
TGLayoutHints *hints = 0;
fCellHintsList = new TList(hints);
fRHdrHintsList = new TList(hints);
fCHdrHintsList = new TList(hints);
fMainHintsList = new TList(hints);
SetBackgroundColor(GetWhitePixel());
fEvenRowBackground = TColor::RGB2Pixel(204, 255, 204);
fOddRowBackground = TColor::RGB2Pixel(255, 255, 255);
fHeaderBackground = TColor::RGB2Pixel(204, 204, 255);
fCurrentRange->fXbr = ncolumns;
fCurrentRange->fYbr = nrows;
Init();
if(fInterface) SetInterface(fInterface, nrows, ncolumns);
SetWindowName();
}
TGTable::~TGTable()
{
UInt_t i = 0, j = 0;
for (i = 0; i < GetNTableRows(); i++) {
for (j = 0; j < GetNTableColumns(); j++) {
delete GetCell(i,j);
}
delete fRows->At(i);
}
delete fRows;
delete fRowHeaders;
delete fColumnHeaders;
delete fCurrentRange;
delete fDataRange;
delete fGotoRange;
fCellHintsList->Delete();
delete fCellHintsList;
delete fRHdrHintsList;
delete fCHdrHintsList;
fMainHintsList->Delete();
delete fMainHintsList;
}
void TGTable::Init()
{
UInt_t nrows = GetNTableRows();
UInt_t ncolumns = GetNTableColumns();
fTopFrame = new TGHorizontalFrame(this, fWidth, fCellHeight);
fTopExtraFrame = new TGHorizontalFrame(fTopFrame, fWidth - fCellWidth,
fCellHeight);
TGString *str = new TGString();
*str += GetNTableRows();
*str += "x";
*str += GetNTableColumns();
*str += " Table";
fTableHeader = new TGTableHeader(fTopFrame, this, str, 0,
kTableHeader);
fBottomFrame = new TGHorizontalFrame(this, fWidth, fHeight - fCellHeight);
fRHdrFrame = new TGTableHeaderFrame(fBottomFrame, this, fCellWidth,
fHeight - fCellHeight, kRowHeader);
fCHdrFrame = new TGTableHeaderFrame(fTopExtraFrame, this, fWidth - fCellWidth,
fCellHeight, kColumnHeader);
fButtonFrame = new TGHorizontalFrame(this, 200, 50);
fNextButton = new TGTextButton(fButtonFrame, "Next", WidgetId() + 2000);
fPrevButton = new TGTextButton(fButtonFrame, "Previous", WidgetId() + 2001);
fUpdateButton = new TGTextButton(fButtonFrame, "Update", WidgetId() + 2002);
fCanvas = new TGCanvas(fBottomFrame, ncolumns * fCellWidth,
nrows * fCellHeight, 0);
fTableFrame = new TGTableFrame(fCanvas->GetViewPort(), nrows, ncolumns);
fTableFrame->SetCanvas(fCanvas);
fCanvas->SetContainer(fTableFrame->GetFrame());
fRangeFrame = new TGHorizontalFrame(this, 450, 50);
fFirstCellLabel = new TGLabel(fRangeFrame, "Top left cell in range:");
fRangeLabel = new TGLabel(fRangeFrame, "Range:");
fFirstCellEntry = new TGTextEntry(fRangeFrame, "0,0", WidgetId() + 2050);
fFirstCellEntry->SetWidth(100);
fFirstCellEntry->SetAlignment(kTextRight);
fFirstCellEntry->Connect("TextChanged(const char *)", "TGTable", this,
"UserRangeChange()");
fFirstCellEntry->Connect("ReturnPressed()", "TGTable", this, "Goto()");
TString range;
range += GetNTableRows();
range += "x";
range += GetNTableColumns();
fRangeEntry = new TGTextEntry(range, fRangeFrame, WidgetId() + 2051);
fRangeEntry->SetWidth(100);
fRangeEntry->SetAlignment(kTextRight);
fRangeEntry->Connect("TextChanged(const char *)", "TGTable", this,
"UserRangeChange()");
fRangeEntry->Connect("ReturnPressed()", "TGTable", this, "Goto()");
fRangeEntry->Connect("TabPressed()", "TGTextEntry", fFirstCellEntry,
"SetFocus()");
fFirstCellEntry->Connect("TabPressed()", "TGTextEntry", fRangeEntry,
"SetFocus()");
fGotoRange->fXbr = GetNTableRows();
fGotoRange->fYbr = GetNTableColumns();
fGotoButton = new TGTextButton(fRangeFrame, "Goto", WidgetId() + 2003);
fGotoButton->SetState(kButtonDisabled);
fCHdrFrame->SetBackgroundColor(fBackground);
fRHdrFrame->SetBackgroundColor(fBackground);
fRangeFrame->SetBackgroundColor(fBackground);
fTopFrame->SetBackgroundColor(fBackground);
fTopExtraFrame->SetBackgroundColor(fBackground);
fBottomFrame->SetBackgroundColor(fBackground);
fButtonFrame->SetBackgroundColor(fBackground);
fFirstCellLabel->SetBackgroundColor(fBackground);
fRangeLabel->SetBackgroundColor(fBackground);
UInt_t i = 0, j = 0;
TGString *label = 0;
fRowHeaders = new TObjArray(nrows);
for(i = 0; i < nrows; i++) {
TGTableHeader *hdr = new TGTableHeader(fRHdrFrame, this,
label, i, kRowHeader);
fRowHeaders->AddAt(hdr, i);
}
fColumnHeaders = new TObjArray(ncolumns);
for(i = 0; i < ncolumns; i++) {
TGTableHeader *hdr = new TGTableHeader(fCHdrFrame, this,
label, i, kColumnHeader);
fColumnHeaders->AddAt(hdr, i);
}
TGTableCell *cell = 0;
TObjArray *row = 0;
fRows = new TObjArray(nrows);
for (i = 0; i < nrows; i++) {
row = new TObjArray(ncolumns);
fRows->AddAt(row, i);
for (j = 0; j < ncolumns; j++) {
cell = new TGTableCell(fCanvas->GetContainer(), this, label, i, j);
row->AddAt(cell, j);
}
}
if ((GetNDataColumns() >= GetNTableColumns()) &&
(GetNDataRows() >= GetNTableRows())) {
fAllData = kTRUE;
} else {
fAllData = kFALSE;
}
TGLayoutHints *lhints = 0;
for (i = 0; i < nrows; i++) {
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop);
fRHdrHintsList->Add(lhints);
fRHdrFrame->AddFrame(GetRowHeader(i), lhints);
for (j = 0; j < ncolumns; j++) {
if (i == 0) {
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop);
fCHdrHintsList->Add(lhints);
fCHdrFrame->AddFrame(GetColumnHeader(j), lhints);
}
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop);
fCellHintsList->Add(lhints);
fCanvas->AddFrame(GetCell(i,j), lhints);
}
}
lhints = new TGLayoutHints(kLHintsRight | kLHintsCenterY, 3, 30, 4, 4);
fRangeFrame->AddFrame(fGotoButton, lhints);
lhints = new TGLayoutHints(kLHintsRight |kLHintsCenterY, 3, 3, 4, 4);
fRangeFrame->AddFrame(fRangeEntry, lhints);
lhints = new TGLayoutHints(kLHintsRight |kLHintsCenterY, 3, 3, 4, 4);
fRangeFrame->AddFrame(fRangeLabel, lhints);
lhints = new TGLayoutHints(kLHintsRight |kLHintsCenterY, 3, 3, 4, 4);
fRangeFrame->AddFrame(fFirstCellEntry, lhints);
lhints = new TGLayoutHints(kLHintsRight |kLHintsCenterY, 3, 3, 4, 4);
fRangeFrame->AddFrame(fFirstCellLabel, lhints);
lhints = new TGLayoutHints(kLHintsRight |kLHintsTop);
fRangeFrame->Resize();
AddFrame(fRangeFrame, lhints);
lhints = new TGLayoutHints(kLHintsLeft |kLHintsTop);
fTopFrame->AddFrame(fTableHeader, lhints);
lhints = new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsTop);
fTopExtraFrame->AddFrame(fCHdrFrame, lhints);
lhints = new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsTop);
fTopFrame->AddFrame(fTopExtraFrame, lhints);
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandY);
fBottomFrame->AddFrame(fRHdrFrame, lhints);
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX
| kLHintsExpandY);
fBottomFrame->AddFrame(fCanvas, lhints);
lhints = new TGLayoutHints(kLHintsRight | kLHintsCenterY, 3, 30, 4, 4);
fButtonFrame->AddFrame(fNextButton, lhints);
lhints = new TGLayoutHints(kLHintsRight | kLHintsCenterY, 3, 3, 4, 4);
fButtonFrame->AddFrame(fPrevButton, lhints);
lhints = new TGLayoutHints(kLHintsRight | kLHintsCenterY, 3, 30, 4, 4);
fButtonFrame->AddFrame(fUpdateButton, lhints);
fButtonFrame->Resize();
fButtonFrame->ChangeOptions(fButtonFrame->GetOptions() | kFixedWidth);
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX);
AddFrame(fTopFrame, lhints);
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX |
kLHintsExpandY);
AddFrame(fBottomFrame, lhints);
lhints = new TGLayoutHints(kLHintsExpandX | kLHintsTop);
AddFrame(fButtonFrame, lhints);
TGScrollBar *sbar= fCanvas->GetVScrollbar();
sbar->Connect("PositionChanged(Int_t)", "TGTable", this, "ScrollRHeaders(Int_t)");
sbar = fCanvas->GetHScrollbar();
sbar->Connect("PositionChanged(Int_t)", "TGTable", this, "ScrollCHeaders(Int_t)");
fUpdateButton->Connect("Clicked()", "TGTable", this, "Update()");
fNextButton->Connect("Clicked()", "TGTable", this, "NextChunk()");
fPrevButton->Connect("Clicked()", "TGTable", this, "PreviousChunk()");
fGotoButton->Connect("Clicked()", "TGTable", this, "Goto()");
}
void TGTable::DoRedraw()
{
MapSubwindows();
Layout();
}
void TGTable::Expand(UInt_t nrows, UInt_t ncolumns)
{
ExpandRows(nrows);
ExpandColumns(ncolumns);
}
void TGTable::ExpandColumns(UInt_t ncolumns)
{
UInt_t i = 0, j = 0;
TGString *label = 0;
UInt_t ntrows = GetNTableRows();
UInt_t ntcolumns = GetNTableColumns();
fColumnHeaders->Expand(ntcolumns + ncolumns);
for (i = 0; i < ncolumns; i++) {
TGTableHeader *header = new TGTableHeader(fCHdrFrame, this, label,
ntcolumns + i,
kColumnHeader);
fColumnHeaders->AddAt(header, ntcolumns + i);
}
for (i = 0; i < ntrows; i++) {
GetRow(i)->Expand(ntcolumns + ncolumns);
for (j = 0; j < ncolumns; j++) {
TGTableCell *cell = new TGTableCell(fCanvas->GetContainer(), this, label, i,
ntcolumns + j);
GetRow(i)->AddAt(cell, ntcolumns + j);
}
}
fCurrentRange->fXbr += ncolumns;
if ((GetNDataColumns() == GetNTableColumns()) &&
(GetNDataRows() == GetNTableRows())) {
fAllData = kTRUE;
} else {
fAllData = kFALSE;
}
}
void TGTable::ExpandRows(UInt_t nrows)
{
UInt_t i = 0, j = 0;
UInt_t ntrows = GetNTableRows();
UInt_t ntcolumns = GetNTableColumns();
fRows->Expand(ntrows + nrows);
fRowHeaders->Expand(ntrows + nrows);
for (i = 0; i < nrows; i++) {
TObjArray *row = new TObjArray(ntcolumns);
fRows->AddAt(row, ntrows + i);
TGString *label = 0;
TGTableHeader *header = new TGTableHeader(fRHdrFrame, this, label,
ntrows + i, kRowHeader);
fRowHeaders->AddAt(header, ntrows + i);
for (j = 0; j < ntcolumns ; j++) {
TGTableCell *cell = new TGTableCell(fCanvas->GetContainer(), this, label,
ntrows + i, j);
GetRow(ntrows + i)->AddAt(cell, j);
}
}
fCurrentRange->fYbr += nrows;
if ((GetNDataColumns() == GetNTableColumns()) &&
(GetNDataRows() == GetNTableRows())) {
fAllData = kTRUE;
} else {
fAllData = kFALSE;
}
}
UInt_t TGTable::GetCHdrWidth() const
{
Int_t ncolumns = GetNTableColumns();
UInt_t width = 0;
for (Int_t i = 0; i < ncolumns; i++) {
width += GetColumnHeader(i)->GetWidth();
}
return width;
}
UInt_t TGTable::GetRHdrHeight() const
{
Int_t nrows = GetNTableRows();
UInt_t height = 0;
for (Int_t i = 0; i < nrows; i++) {
height += GetRowHeader(i)->GetHeight();
}
return height;
}
void TGTable::Shrink(UInt_t nrows, UInt_t ncolumns)
{
ShrinkRows(nrows);
ShrinkColumns(ncolumns);
}
void TGTable::ShrinkColumns(UInt_t ncolumns)
{
UInt_t i = 0, j = 0, k = 0;
if(GetNTableColumns() - ncolumns < 1) {
Info("TGTable::ShrinkColumns", "Cannot shrink smaller than 1"
" column, adjusting");
ncolumns = GetNTableColumns() - 1;
}
UInt_t ntrows = GetNTableRows();
UInt_t ntcolumns = GetNTableColumns();
TGTableCell *cell = 0;
for (i = 0; i < ntrows; i++) {
for (j = 0; j < ncolumns; j++) {
k = ntcolumns - ncolumns + j;
cell = (TGTableCell *)GetRow(i)->RemoveAt(k);
cell->DestroyWindow();
delete cell;
}
GetRow(i)->Expand(ntcolumns - ncolumns);
}
TGTableHeader *hdr = 0;
for (j = 0; j < ncolumns; j++) {
hdr = (TGTableHeader *)fColumnHeaders->RemoveAt(ntcolumns - ncolumns + j);
hdr->DestroyWindow();
delete hdr;
}
fColumnHeaders->Expand(ntcolumns - ncolumns);
fCurrentRange->fXbr -= ncolumns;
if ((GetNDataColumns() == GetNTableColumns()) &&
(GetNDataRows() == GetNTableRows())) {
fAllData = kTRUE;
} else {
fAllData = kFALSE;
}
}
void TGTable::ShrinkRows(UInt_t nrows)
{
UInt_t i = 0 , j = 0;
if(GetNTableRows() - nrows < 1) {
Info("TGTable::ShrinkRows", "Cannot shrink smaller than 1 row, adjusting");
nrows = GetNTableRows() - 1;
}
UInt_t ntrows = GetNTableRows();
UInt_t ntcolumns = GetNTableColumns();
TObjArray *row = 0;
TGTableCell *cell = 0;
TGTableHeader *hdr = 0;
for (i = 0; i < nrows; i++) {
for (j = 0; j < ntcolumns ; j++) {
cell = (TGTableCell *)GetRow(ntrows - nrows + i)->RemoveAt(j);
cell->DestroyWindow();
delete cell;
}
row = (TObjArray *)fRows->RemoveAt(ntrows - nrows + i);
delete row;
hdr = (TGTableHeader *)fRowHeaders->RemoveAt(ntrows - nrows + i);
hdr->DestroyWindow();
delete hdr;
}
fRows->Expand(ntrows - nrows);
fRowHeaders->Expand(ntrows - nrows);
fCurrentRange->fYbr -= nrows;
if ((GetNDataColumns() == GetNTableColumns()) &&
(GetNDataRows() == GetNTableRows())) {
fAllData = kTRUE;
} else {
fAllData = kFALSE;
}
}
void TGTable::UpdateHeaders(EHeaderType type)
{
UInt_t max = 0, i = 0, d = 0;
if(type == kColumnHeader) {
max = GetNTableColumns();
for (i = 0; i < max; i++) {
d = fCurrentRange->fXtl + i;
GetColumnHeader(i)->SetLabel(fInterface->GetColumnHeader(d));
}
} else if (type == kRowHeader) {
max = GetNTableRows();
for (i = 0; i < max; i++) {
d = fCurrentRange->fYtl + i;
GetRowHeader(i)->SetLabel(fInterface->GetRowHeader(d));
}
}
}
void TGTable::SetInterface(TVirtualTableInterface *interface,
UInt_t nrows, UInt_t ncolumns)
{
fInterface = interface;
fDataRange->fXtl = 0;
fDataRange->fYtl = 0;
fDataRange->fXbr = fInterface->GetNColumns();
fDataRange->fYbr = fInterface->GetNRows();
UInt_t x = 0, y = 0;
if (fDataRange->fXbr < ncolumns) {
x = fDataRange->fXbr;
} else {
x = ncolumns;
}
if (fDataRange->fYbr < nrows) {
y = fDataRange->fYbr;
} else {
y = nrows;
}
GotoTableRange(0, 0, x, y);
if ((GetNDataColumns() == GetNTableColumns()) &&
(GetNDataRows() == GetNTableRows())) {
fAllData = kTRUE;
} else {
fAllData = kFALSE;
}
}
void TGTable::ResizeTable(UInt_t newnrows, UInt_t newncolumns)
{
UInt_t oldnrows = GetNTableRows();
UInt_t oldncolumns = GetNTableColumns();
UInt_t oldwidth = 0, oldheight = 0;
Int_t i = 0, j = 0;
oldwidth = GetCHdrWidth();
oldheight = GetRHdrHeight();
TGCompositeFrame *container = (TGCompositeFrame *)fCanvas->GetContainer();
if (newnrows != oldnrows){
if (newnrows > oldnrows) {
ExpandRows(newnrows - oldnrows);
} else {
ShrinkRows(oldnrows - newnrows);
}
}
if (newncolumns != oldncolumns){
if (newncolumns > oldncolumns) {
ExpandColumns(newncolumns - oldncolumns);
} else {
ShrinkColumns(oldncolumns - newncolumns);
}
}
if ((newncolumns != oldncolumns) || (newnrows != oldnrows)) {
container->RemoveAll();
fCellHintsList->Delete();
fRHdrFrame->RemoveAll();
fRHdrHintsList->Delete();
fCHdrFrame->RemoveAll();
fCHdrHintsList->Delete();
container->SetLayoutManager(new TGMatrixLayout(container,
newnrows, newncolumns));
TGLayoutHints *lhints = 0;
for (i = 0; i < (Int_t)newnrows; i++) {
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop);
fRHdrHintsList->Add(lhints);
fRHdrFrame->AddFrame(GetRowHeader(i), lhints);
for (j = 0; j < (Int_t)newncolumns; j++) {
if (i == 0) {
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop);
fCHdrHintsList->Add(lhints);
fCHdrFrame->AddFrame(GetColumnHeader(j), lhints);
}
lhints = new TGLayoutHints(kLHintsLeft | kLHintsTop);
fCellHintsList->Add(lhints);
fCanvas->AddFrame(GetCell(i,j), lhints);
}
}
}
fCanvas->MapSubwindows();
fCanvas->Layout();
}
void TGTable::UpdateRangeFrame()
{
TString tl, range;
tl += fCurrentRange->fYtl;
tl += ",";
tl += fCurrentRange->fXtl;
fFirstCellEntry->SetText(tl.Data());
range += GetNTableRows();
range += "x";
range += GetNTableColumns();
fRangeEntry->SetText(range.Data());
fGotoButton->SetState(kButtonDisabled);
}
TObjArray *TGTable::GetRow(UInt_t row)
{
return (TObjArray *)fRows->At(row);
}
TObjArray *TGTable::GetColumn(UInt_t column)
{
UInt_t nrows = GetNTableRows();
TObjArray *col = new TObjArray(nrows);
for(UInt_t ui = 0; ui < nrows; ui++) {
col->AddAt(GetCell(ui, column), ui);
}
return col;
}
const TGTableCell* TGTable::GetCell(UInt_t i, UInt_t j) const
{
return const_cast<TGTable *>(this)->GetCell(i, j);
}
TGTableCell* TGTable::GetCell(UInt_t i, UInt_t j)
{
TObjArray *row = (TObjArray *)fRows->At(i);
if(row) {
TGTableCell *cell = (TGTableCell *)row->At(j);
return cell;
} else {
return 0;
}
}
const TGTableCell* TGTable::FindCell(TGString label) const
{
return const_cast<TGTable *>(this)->FindCell(label);
}
TGTableCell* TGTable::FindCell(TGString label)
{
TObjArray *row = 0;
TGTableCell *cell = 0;
UInt_t i = 0, j = 0;
UInt_t nrows = GetNTableRows();
UInt_t ncolumns = GetNTableColumns();
for (i = 0; i < nrows; i++) {
for (j = 0; j < ncolumns; j++) {
row = (TObjArray *)fRows->At(j);
cell = (TGTableCell *)row->At(i);
if (*(cell->GetLabel()) == label) {
return cell;
}
}
}
return 0;
}
void TGTable::Show()
{
TGTableCell *cell = 0;
TGTableHeader *hdr = 0;
UInt_t i = 0, j = 0;
UInt_t nrows = GetNTableRows();
UInt_t ncolumns = GetNTableColumns();
std::ios_base::fmtflags org_flags = std::cout.flags();
for (j = 0; j < ncolumns + 1; j++) {
if (j == 0) {
hdr = fTableHeader;
if (hdr) std::cout << " " << setw(12) << right
<< hdr->GetLabel()->GetString() << " ";
} else {
hdr = GetColumnHeader(j - 1);
if (hdr) std::cout << " " << setw(12) << right
<< hdr->GetLabel()->GetString() << " ";
}
}
std::cout << std::endl;
for (i = 0; i < nrows; i++) {
for (j = 0; j < ncolumns + 1; j++) {
if (j == 0) {
hdr = GetRowHeader(i);
if (hdr) std::cout << " " << setw(12) << right
<< hdr->GetLabel()->GetString() << " ";
} else {
cell = GetCell(i, j - 1);
if (cell) std::cout << " " << setw(12) << right
<< cell->GetLabel()->GetString() << " ";
}
}
std::cout << std::endl;
}
std::cout.flags(org_flags);
}
void TGTable::UpdateView()
{
UInt_t nrows = GetNTableRows();
UInt_t ncolumns = GetNTableColumns();
TGString *str = new TGString();
*str += nrows;
*str += "x";
*str += ncolumns;
*str += " Table";
fTableHeader->SetLabel(str->GetString());
delete str;
UpdateHeaders(kRowHeader);
UpdateHeaders(kColumnHeader);
UInt_t i = 0, j = 0;
UInt_t k = 0, l = 0;
TGTableCell * cell = 0;
for (i = 0; i < nrows; i++) {
for (j = 0; j < ncolumns; j++) {
cell = GetCell(i,j);
k = fCurrentRange->fYtl + i;
l = fCurrentRange->fXtl + j;
const char *label = fInterface->GetValueAsString(k,l);
if(cell) cell->SetLabel(label);
}
}
MapSubwindows();
Layout();
gClient->NeedRedraw(fTableHeader);
TGViewPort *vp = fCanvas->GetViewPort();
fTableFrame->DrawRegion(0, 0, vp->GetWidth(), vp->GetHeight());
fCHdrFrame->DrawRegion(0, 0, fCHdrFrame->GetWidth(), fCHdrFrame->GetHeight());
fRHdrFrame->DrawRegion(0, 0, fRHdrFrame->GetWidth(), fRHdrFrame->GetHeight());
UpdateRangeFrame();
}
UInt_t TGTable::GetNTableRows() const
{
return fCurrentRange->fYbr - fCurrentRange->fYtl;
}
UInt_t TGTable::GetNDataRows() const
{
return fDataRange->fYbr - fDataRange->fYtl;
}
UInt_t TGTable::GetNTableColumns() const
{
return fCurrentRange->fXbr - fCurrentRange->fXtl;
}
UInt_t TGTable::GetNDataColumns() const
{
return fDataRange->fYbr - fDataRange->fYtl;
}
UInt_t TGTable::GetNTableCells() const
{
return GetNTableRows() * GetNTableColumns();
}
UInt_t TGTable::GetNDataCells() const
{
return GetNDataRows() * GetNDataColumns();
}
const TTableRange *TGTable::GetCurrentRange() const
{
return fCurrentRange;
}
const TGTableHeader *TGTable::GetRowHeader(const UInt_t row) const
{
return const_cast<TGTable *>(this)->GetRowHeader(row);
}
TGTableHeader *TGTable::GetRowHeader(const UInt_t row)
{
return (TGTableHeader *)fRowHeaders->At(row);
}
const TGTableHeader *TGTable::GetColumnHeader(const UInt_t column) const
{
return const_cast<TGTable *>(this)->GetColumnHeader(column);
}
TGTableHeader *TGTable::GetColumnHeader(const UInt_t column)
{
return (TGTableHeader *)fColumnHeaders->At(column);
}
TGTableHeader *TGTable::GetTableHeader()
{
return fTableHeader;
}
Pixel_t TGTable::GetRowBackground(UInt_t row) const
{
if (row % 2 == 0) {
return fEvenRowBackground;
} else {
return fOddRowBackground;
}
}
Pixel_t TGTable::GetHeaderBackground() const
{
return fHeaderBackground;
}
void TGTable::SetOddRowBackground(Pixel_t pixel)
{
if(pixel == fOddRowBackground) return;
fOddRowBackground = pixel;
UInt_t nrows = GetNTableRows();
UInt_t ncolumns = GetNTableColumns();
UInt_t i = 0, j = 0;
TGTableCell *cell = 0;
for (i = 0; i < nrows; i++) {
for (j = 0; j < ncolumns; j++) {
if (i % 2) {
cell = GetCell(i,j);
cell->SetBackgroundColor(fOddRowBackground);
}
}
}
UInt_t width = fCanvas->GetViewPort()->GetWidth();
UInt_t height = fCanvas->GetViewPort()->GetHeight();
fTableFrame->DrawRegion(0, 0, width, height);
}
void TGTable::SetEvenRowBackground(Pixel_t pixel)
{
if(pixel == fEvenRowBackground) return;
fEvenRowBackground = pixel;
UInt_t nrows = GetNTableRows();
UInt_t ncolumns = GetNTableColumns();
UInt_t i = 0, j = 0;
TGTableCell *cell = 0;
for (i = 0; i < nrows; i++) {
for (j = 0; j < ncolumns; j++) {
if (!(i % 2)) {
cell = GetCell(i,j);
cell->SetBackgroundColor(fEvenRowBackground);
}
}
}
UInt_t width = fCanvas->GetViewPort()->GetWidth();
UInt_t height = fCanvas->GetViewPort()->GetHeight();
fTableFrame->DrawRegion(0, 0, width, height);
}
void TGTable::SetHeaderBackground(Pixel_t pixel)
{
if(pixel == fHeaderBackground) return;
fHeaderBackground = pixel;
UInt_t nrows = GetNTableRows();
UInt_t ncolumns = GetNTableColumns();
UInt_t i = 0, j = 0;
TGTableHeader *hdr = 0;
for (i = 0; i < nrows; i++) {
hdr = GetRowHeader(i);
hdr->SetBackgroundColor(fHeaderBackground);
}
UInt_t height = fCanvas->GetViewPort()->GetHeight();
UInt_t width = fTableHeader->GetWidth();
fRHdrFrame->DrawRegion(0, 0, width, height);
for (j = 0; j < ncolumns; j++) {
hdr = GetColumnHeader(j);
hdr->SetBackgroundColor(fHeaderBackground);
}
width = fCanvas->GetViewPort()->GetWidth();
height = fTableHeader->GetHeight();
fCHdrFrame->DrawRegion(0, 0, width, height);
}
void TGTable::SetDefaultColors()
{
SetEvenRowBackground(TColor::RGB2Pixel(204, 255, 204));
SetOddRowBackground(TColor::RGB2Pixel(255, 255, 255));
SetHeaderBackground(TColor::RGB2Pixel(204, 204, 255));
}
void TGTable::MoveTable(Int_t rows, Int_t columns)
{
if (fAllData) return;
Int_t xtl = fCurrentRange->fXtl + columns;
Int_t ytl = fCurrentRange->fYtl + rows;
Int_t xbr = fCurrentRange->fXbr + columns;
Int_t ybr = fCurrentRange->fYbr + rows;
GotoTableRange(xtl, ytl, xbr, ybr);
}
void TGTable::GotoTableRange(Int_t xtl, Int_t ytl, Int_t xbr, Int_t ybr)
{
if (fAllData) return;
if(xtl == xbr || ytl == ybr) {
Error("TGTable::GotoTableRange","x or y range = 0");
return;
}
Int_t nrows = TMath::Abs(ybr - ytl);
Int_t ncolumns = TMath::Abs(xbr - xtl);
if (xtl > xbr) {
Info("TGTable::GotoTableRange","Swapping x-range boundries");
Int_t temp = xtl;
xtl = xbr;
xbr = temp;
}
if (ytl > ybr) {
Info("TGTable::GotoTableRange","Swapping y-range boundries");
Int_t temp = ytl;
ytl = ybr;
ybr = temp;
}
if((xtl < 0) || (xbr < 0)) {
Info("TGTable::GotoTableRange", "Column boundry out of bounds, adjusting");
xtl = 0;
xbr = ncolumns;
if (xbr > (Int_t)fDataRange->fXbr) {
xbr = fDataRange->fXbr;
ncolumns = TMath::Abs(xbr - xtl);
}
}
if((ytl < 0) || (ybr < 0)) {
Info("TGTable::GotoTableRange", "Row boundry out of bounds, adjusting");
ytl = 0;
ybr = nrows;
if (ybr > (Int_t)fDataRange->fYbr) {
ybr = fDataRange->fYbr;
nrows = TMath::Abs(ybr - ytl);
}
}
if((xtl > (Int_t)fDataRange->fXbr) || (xbr > (Int_t)fDataRange->fXbr)) {
Info("TGTable::GotoTableRange", "Left Column boundry out of bounds, "
"adjusting");
xbr = fDataRange->fXbr;
xtl = xbr - ncolumns;
if (xtl < 0) {
xtl = 0;
ncolumns = TMath::Abs(xbr - xtl);
Info("TGTable::GotoTableRange", "Right column boundry out of"
" bounds, set to 0");
}
}
if ((ytl > (Int_t)fDataRange->fYbr) || (ybr > (Int_t)fDataRange->fYbr)) {
Info("TGTable::GotoTableRange", "Bottom row boundry out of bounds, "
"adjusting");
ybr = fDataRange->fYbr;
ytl = ybr - nrows;
if (ytl < 0) {
ytl = 0;
nrows = ybr - ytl;
Info("TGTable::GotoTableRange", "Top row boundry out of bounds, "
"set to 0");
}
}
nrows = TMath::Abs(ybr - ytl);
ncolumns = TMath::Abs(xbr - xtl);
ResizeTable(nrows, ncolumns);
fCurrentRange->fXtl = xtl;
fCurrentRange->fYtl = ytl;
fCurrentRange->fXbr = xbr;
fCurrentRange->fYbr = ybr;
UpdateView();
}
TGTableCell *TGTable::operator() (UInt_t row, UInt_t column)
{
return GetCell(row, column);
}
void TGTable::ScrollCHeaders(Int_t xpos)
{
if (!fCHdrFrame) return;
fCHdrFrame->Move(- xpos, 0);
fCHdrFrame->Resize();
fCHdrFrame->DrawRegion(0, 0, fCHdrFrame->GetWidth(),
fCHdrFrame->GetHeight());
}
void TGTable::ScrollRHeaders(Int_t ypos)
{
if (!fRHdrFrame) return;
fRHdrFrame->Move(fRHdrFrame->GetX(), -ypos);
fRHdrFrame->Resize();
fRHdrFrame->DrawRegion(0, 0, fRHdrFrame->GetWidth(),
fRHdrFrame->GetHeight());
}
void TGTable::NextChunk()
{
MoveTable(GetNTableRows(), 0);
UpdateRangeFrame();
}
void TGTable::PreviousChunk()
{
MoveTable(-1 * (Int_t)GetNTableRows(), 0);
UpdateRangeFrame();
}
void TGTable::Goto()
{
if (fGotoButton->GetState() == kButtonUp) {
GotoTableRange(fGotoRange->fXtl, fGotoRange->fYtl,
fGotoRange->fXbr, fGotoRange->fYbr);
UpdateRangeFrame();
}
}
void TGTable::UserRangeChange()
{
TString topleft(fFirstCellEntry->GetText());
if(!topleft.Contains(",")) return;
Int_t pos = topleft.First(',');
TString itl = topleft(0,pos);
TString jtl = topleft(pos+1, topleft.Length());
if (itl.Contains(' ') || itl.Contains('\t') ||
jtl.Contains(' ') || jtl.Contains('\t')) return;
if (!itl.IsAlnum() || !jtl.IsAlnum()) return;
fGotoRange->fXtl = jtl.Atoi();
fGotoRange->fYtl = itl.Atoi();
TString range(fRangeEntry->GetText());
if(!range.Contains("x")) return;
pos = 0;
pos = range.First('x');
TString ir = range(0,pos);
TString jr = range(pos+1, range.Length());
if (ir.Contains(' ') || ir.Contains('\t') ||
jr.Contains(' ') || jr.Contains('\t')) return;
if (!ir.IsAlnum() || !jr.IsAlnum()) return;
fGotoRange->fXbr = jtl.Atoi() + jr.Atoi();
fGotoRange->fYbr = itl.Atoi() + ir.Atoi();
if (*fGotoRange == *fCurrentRange) {
fGotoButton->SetState(kButtonDisabled);
} else {
fGotoButton->SetState(kButtonUp);
}
}
void TGTable::Update()
{
fDataRange->fXbr = fInterface->GetNColumns();
fDataRange->fYbr = fInterface->GetNRows();
GotoTableRange(fCurrentRange->fXtl, fCurrentRange->fYtl,
fCurrentRange->fXbr, fCurrentRange->fYbr);
UpdateView();
}
TTableRange::TTableRange() : fXtl(0), fYtl(0), fXbr(0), fYbr(0)
{
}
void TTableRange::Print()
{
std::cout << "Range = (" << fXtl << "," << fYtl << ")->("
<< fXbr << "," << fYbr << ")" << std::endl;
}
Bool_t TTableRange::operator==(TTableRange &other)
{
if ((fXtl == other.fXtl) && (fYtl == other.fYtl) &&
(fXbr == other.fXbr) && (fYbr == other.fYbr)) {
return kTRUE;
} else {
return kFALSE;
}
}