CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWTableWidget.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: TableWidget
4 // Class : FWTableWidget
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Mon Feb 2 16:45:42 EST 2009
11 // $Id: FWTableWidget.cc,v 1.25 2012/02/22 00:15:44 amraktad Exp $
12 //
13 
14 // system include files
15 #include <iostream>
16 #include "TGScrollBar.h"
17 #include "TGTableLayout.h"
18 #include "TGResourcePool.h"
19 
20 
21 // user include files
27 
28 static const unsigned long kWidgetColor = 0x2f2f2f;
29 
30 //
31 // constants, enums and typedefs
32 //
33 static const UInt_t kRowOptions = kLHintsExpandX|kLHintsFillX|kLHintsShrinkX;
34 static const UInt_t kColOptions = kLHintsExpandY|kLHintsFillY|kLHintsShrinkY;
35 
36 //
37 // static data member definitions
38 //
39 
40 //
41 // constructors and destructor
42 //
44 TGCompositeFrame(p),
45  m_bodyTable(iManager),
46  m_headerTable(iManager->hasLabelHeaders()?new FWAdapterHeaderTableManager(iManager): static_cast<FWTableManagerBase*>(0)),
47  m_rowHeaderTable(iManager->hasRowHeaders()?new FWAdapterRowHeaderTableManager(iManager): static_cast<FWTableManagerBase*>(0)),
48  m_header(0),
49  m_rowHeader(0),
50  m_showingVSlider(true),
51  m_showingHSlider(true),
52  m_sortedColumn(-1),
53  m_descendingSort(true),
54  m_forceLayout(false),
55  m_headerBackground(0),
56  m_headerForeground(0),
57  m_lineSeparator(0)
58 {
59  SetLayoutManager( new TGTableLayout(this,3,3) );
60 
61  if(0!=m_headerTable) {
63  AddFrame(m_header, new TGTableLayoutHints(1,2,0,1,kLHintsTop|kLHintsLeft|kRowOptions));
64  if (m_bodyTable->cellDataIsSortable()) m_header->Connect("buttonReleased(Int_t,Int_t,Event_t*,Int_t,Int_t)","FWTableWidget",this,"buttonReleasedInHeader(Int_t,Int_t,Event_t*,Int_t,Int_t)");
65  }
66  m_body = new FWTabularWidget(iManager,this,GetWhiteGC()());
67  //m_body->SetBackgroundColor(kWidgetColor);
68  AddFrame(m_body, new TGTableLayoutHints(1,2,1,2,kLHintsTop|kLHintsLeft|kRowOptions|kColOptions));
69  m_body->Connect("buttonReleased(Int_t,Int_t,Event_t*,Int_t,Int_t)","FWTableWidget",this,"buttonReleasedInBody(Int_t,Int_t,Event_t*,Int_t,Int_t)");
70 
71  //set sizes
72  std::vector<unsigned int> columnWidths = m_body->widthOfTextInColumns();
73  if(0!=m_header) {
74  std::vector<unsigned int> headerWidths = m_header->widthOfTextInColumns();
75  for(std::vector<unsigned int>::iterator it = columnWidths.begin(), itEnd=columnWidths.end(), itHeader=headerWidths.begin();
76  it != itEnd;
77  ++it,++itHeader) {
78  if(*itHeader > *it) {
79  *it = *itHeader;
80  }
81  }
82  }
83  if(0!=m_header) {
84  m_header->setWidthOfTextInColumns(columnWidths);
85  }
86  m_body->setWidthOfTextInColumns(columnWidths);
87  if(m_rowHeaderTable) {
88  m_rowHeader = new FWTabularWidget(m_rowHeaderTable,this, GetWhiteGC()());
89  //m_rowHeader->SetBackgroundColor(kWidgetColor);
90 
91  AddFrame(m_rowHeader, new TGTableLayoutHints(0,1,1,2,kLHintsTop|kLHintsLeft|kColOptions));
92  m_rowHeader->Connect("buttonReleased(Int_t,Int_t,Event_t*,Int_t,Int_t)","FWTableWidget",this,"buttonReleasedInBody(Int_t,Int_t,Event_t*,Int_t,Int_t)");
93  m_rowHeader->Connect("buttonReleased(Int_t,Int_t,Event_t*,Int_t,Int_t)","FWTableWidget",this,"buttonReleasedInRowHeader(Int_t,Int_t,Event_t*,Int_t,Int_t)");
95  }
96 
97  m_hSlider = new TGHScrollBar(this);
98  AddFrame(m_hSlider, new TGTableLayoutHints(1,2,2,3,kRowOptions));
99  m_hSlider->Connect("ProcessedEvent(Event_t*)", "FWTableWidget", this, "childrenEvent(Event_t *)");
100  m_vSlider = new TGVScrollBar(this);
101  m_vSlider->SetSmallIncrement(12);
102  AddFrame(m_vSlider, new TGTableLayoutHints(2,3,1,2,kColOptions));
103  m_vSlider->Connect("ProcessedEvent(Event_t*)", "FWTableWidget", this, "childrenEvent(Event_t *)");
104  MapSubwindows();
105  Layout();
106  //HideFrame(m_hSlider);
107  //HideFrame(m_vSlider);
108  m_hSlider->Associate(this);
109  m_vSlider->Associate(this);
110 
111  m_hSlider->SetEditDisabled(kEditDisable | kEditDisableGrab | kEditDisableBtnEnable);
112  m_vSlider->SetEditDisabled(kEditDisable | kEditDisableGrab | kEditDisableBtnEnable);
113  m_bodyTable->Connect("dataChanged()","FWTableWidget",this,"dataChanged()");
114 }
115 
116 // FWTableWidget::FWTableWidget(const FWTableWidget& rhs)
117 // {
118 // // do actual copying here;
119 // }
120 
122 {
123  if(0!=m_headerBackground) {
124  gClient->GetResourcePool()->GetGCPool()->FreeGC(m_headerBackground->GetGC());
125  }
126  if(0!= m_headerForeground) {
127  gClient->GetResourcePool()->GetGCPool()->FreeGC(m_headerForeground->GetGC());
128  }
129 
130  if(0!= m_lineSeparator) {
131  gClient->GetResourcePool()->GetGCPool()->FreeGC(m_lineSeparator->GetGC());
132  }
133 
134 }
135 
136 //
137 // assignment operators
138 //
139 // const FWTableWidget& FWTableWidget::operator=(const FWTableWidget& rhs)
140 // {
141 // //An exception safe implementation is
142 // FWTableWidget temp(rhs);
143 // swap(rhs);
144 //
145 // return *this;
146 // }
147 
148 //
149 // member functions
150 //
151 void
152 FWTableWidget::sort(UInt_t iColumn, bool iDescendingSort)
153 {
154  if(0!=m_headerTable) {
155  m_headerTable->sort(iColumn,iDescendingSort);
156  }
157  m_bodyTable->sort(iColumn,iDescendingSort);
158  m_sortedColumn =iColumn;
159  m_descendingSort=iDescendingSort;
160 
161  //fClient->NeedRedraw(m_header);
162  //fClient->NeedRedraw(m_body);
163 }
164 
165 void
167 {
168  TGFrame::SetBackgroundColor(iColor);
169  if(m_rowHeaderTable) {
170  m_rowHeader->SetBackgroundColor(iColor);
171  fClient->NeedRedraw(m_rowHeader);
172  }
173  if(m_header) {
174  m_header->SetBackgroundColor(iColor);
175  fClient->NeedRedraw(m_header);
176  }
177  m_body->SetBackgroundColor(iColor);
178  fClient->NeedRedraw(m_body);
179  fClient->NeedRedraw(this);
180 }
181 
182 void
184 {
185  if(0==m_headerBackground) {
186  GCValues_t t = *(gClient->GetResourcePool()->GetFrameGC()->GetAttributes());
187  m_headerBackground = gClient->GetResourcePool()->GetGCPool()->GetGC(&t,kTRUE);
188  }
189  m_headerBackground->SetForeground(iColor);
190  if(0!=m_header) {
192  }
193 }
194 void
196 {
197  if(0==m_headerForeground) {
198  GCValues_t t = *(gClient->GetResourcePool()->GetFrameGC()->GetAttributes());
199  m_headerForeground = gClient->GetResourcePool()->GetGCPool()->GetGC(&t,kTRUE);
200  }
201  m_headerForeground->SetForeground(iColor);
202  if(0!=m_header) {
204  }
205 }
206 
207 void
209 {
210  if(0==m_lineSeparator) {
211  GCValues_t t = *(gClient->GetResourcePool()->GetFrameGC()->GetAttributes());
212  m_lineSeparator = gClient->GetResourcePool()->GetGCPool()->GetGC(&t,kTRUE);
213  }
214  m_lineSeparator->SetForeground(iColor);
216  if(m_rowHeader) {
218  }
219 }
220 
221 
222 void
224 {
225  handleResize(w,h);
226  TGCompositeFrame::Resize(w,h);
227 }
228 
229 bool
231 {
232  //std::cout <<"Resize"<<std::endl;
233  bool redoLayout=false;
234 
235  TGDimension def = m_body->GetDefaultSize();
236  UInt_t fullWidth = def.fWidth;
237  if(m_rowHeader) {
238  fullWidth += m_rowHeader->GetDefaultSize().fWidth;
239  }
240 
241  UInt_t headerHeight = 0;
242  if(m_header) {
243  headerHeight = m_header->GetHeight();
244  }
245  UInt_t fullHeight = def.fHeight + headerHeight;
246 
247  UInt_t sBarWidth = (h < fullHeight) ? m_vSlider->GetWidth() : 0;
248  UInt_t sBarHeight = (w < fullWidth) ? m_hSlider->GetHeight() : 0;
249  if (sBarWidth == 0 && sBarHeight > 0 && h < fullHeight + sBarHeight)
250  sBarWidth = m_vSlider->GetWidth();
251  else if (sBarHeight == 0 && sBarWidth > 0 && h < fullWidth + sBarWidth)
252  sBarHeight = m_hSlider->GetHeight();
253  fullWidth += sBarWidth;
254  fullHeight += sBarHeight;
255 
256  if(w < fullWidth) {
257  if(!m_showingHSlider) {
258  ShowFrame(m_hSlider);
259  redoLayout=true;
260  m_showingHSlider=true;
261  }
262  m_hSlider->SetRange(fullWidth,w);
263  } else {
264  if(m_showingHSlider) {
265  HideFrame(m_hSlider);
266  m_hSlider->SetPosition(0);
267  m_showingHSlider = false;
268  redoLayout=true;
269  }
270  }
271 
272  if(h < fullHeight) {
273  if(!m_showingVSlider) {
274  ShowFrame(m_vSlider);
275  m_showingVSlider=true;
276  redoLayout=true;
277  }
278  m_vSlider->SetRange(fullHeight,h);
279  } else {
280  if(m_showingVSlider) {
281  HideFrame(m_vSlider);
282  m_vSlider->SetPosition(0);
283  m_showingVSlider = false;
284  redoLayout=true;
285  }
286  }
287  if(redoLayout) {
288  Layout();
289  }
290 
291  return redoLayout;
292 }
293 
294 void
296 {
297  //std::cout <<"MoveResize"<<std::endl;
298  if(w != GetWidth() || h != GetHeight()) {
299  handleResize(w,h);
300  }
301  TGCompositeFrame::MoveResize(x,y,w,h);
302 }
303 
304 Bool_t
305 FWTableWidget::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
306 {
307  // Handle message generated by the canvas scrollbars.
308 
309  switch (GET_MSG(msg)) {
310  case kC_HSCROLL:
311  switch (GET_SUBMSG(msg)) {
312  case kSB_SLIDERTRACK:
313  case kSB_SLIDERPOS:
314  m_body->setHorizontalOffset(parm1);
315  if(m_header) {
317  }
318  break;
319  }
320  break;
321 
322  case kC_VSCROLL:
323  switch (GET_SUBMSG(msg)) {
324  case kSB_SLIDERTRACK:
325  case kSB_SLIDERPOS:
326  m_body->setVerticalOffset(parm1);
327  if(m_rowHeader) {
329  }
330  break;
331  }
332  break;
333 
334  default:
335  break;
336  }
337  return kTRUE;
338 }
339 
340 void
341 FWTableWidget::buttonReleasedInHeader(Int_t row, Int_t column, Event_t* event,Int_t,Int_t)
342 {
343  Int_t btn = event->fCode;
344  Int_t keyMod = event->fState;
345  //Int_t keyMod = event->fState;
346  if (btn == kButton1 || btn == kButton3) {
347  if(m_sortedColumn==column) {
348  sort(column, !m_descendingSort);
349  } else {
350  sort(column,true);
351  }
352  }
353  columnClicked(column, btn, keyMod);
354 }
355 
356 void
357 FWTableWidget::buttonReleasedInBody(Int_t row, Int_t column, Event_t* event,Int_t iRelX,Int_t iRelY)
358 {
359  Int_t btn = event->fCode;
360  Int_t keyMod = event->fState;
361  if(btn == kButton5){
362  //should scroll down
363  if(m_vSlider) {
364  Int_t p = m_vSlider->GetPosition();
365  Int_t mx = m_vSlider->GetRange();
366  p+=m_vSlider->GetSmallIncrement();
367  if(p>mx){ p=mx;}
368  m_vSlider->SetPosition(p);
369  }
370  return;
371  }
372  if(btn == kButton4){
373  //should scroll up
374  if(m_vSlider) {
375  Int_t p = m_vSlider->GetPosition();
376  p -=m_vSlider->GetSmallIncrement();
377  if(0>p) {p=0;}
378  m_vSlider->SetPosition(p);
379  }
380  return;
381  }
382  if(btn != kButton1 && btn != kButton3) {return;}
383  if(row>=-1 and row < m_bodyTable->numberOfRows()) {
384  Int_t globalX,globalY;
385  Window_t childdum;
386  gVirtualX->TranslateCoordinates(m_body->GetId(),
387  gClient->GetDefaultRoot()->GetId(),
388  event->fX,event->fY,globalX,globalY,childdum);
389  cellClicked(m_bodyTable->unsortedRowNumber(row), column, btn, keyMod, globalX, globalY);
390  rowClicked(m_bodyTable->unsortedRowNumber(row), btn,keyMod,globalX,globalY);
391  }
392 }
393 
394 void
395 FWTableWidget::cellClicked(Int_t row, Int_t column, Int_t btn, Int_t keyMod, Int_t iGlobalX, Int_t iGlobalY)
396 {
397  keyMod = (keyMod&(kKeyShiftMask|kKeyControlMask));
398  //std::cout <<"rowClicked "<<row<<" "<<btn<<" "<<keyMod<<std::endl;
399  Long_t args[6];
400  args[0]=(Long_t)row;
401  args[1]=(Long_t)column;
402  args[2]=(Long_t)btn;
403  args[3]=(Long_t)keyMod;
404  args[4]=(Long_t)iGlobalX;
405  args[5]=(Long_t)iGlobalY;
406  Emit("cellClicked(Int_t,Int_t,Int_t,Int_t,Int_t,Int_t)",args);
407 }
408 
409 void
411 {
412  Clicked();
413 }
414 
415 void
417 {
418  Emit("Clicked()");
419 }
420 
421 void
422 FWTableWidget::rowClicked(Int_t row, Int_t btn, Int_t keyMod, Int_t iGlobalX, Int_t iGlobalY)
423 {
424  keyMod = (keyMod&(kKeyShiftMask|kKeyControlMask));
425  //std::cout <<"rowClicked "<<row<<" "<<btn<<" "<<keyMod<<std::endl;
426  Long_t args[5];
427  args[0]=(Long_t)row;
428  args[1]=(Long_t)btn;
429  args[2]=(Long_t)keyMod;
430  args[3]=(Long_t)iGlobalX;
431  args[4]=(Long_t)iGlobalY;
432  Emit("rowClicked(Int_t,Int_t,Int_t,Int_t,Int_t)",args);
433 }
434 
435 void
436 FWTableWidget::columnClicked(Int_t column, Int_t btn, Int_t keyMod)
437 {
438  keyMod = (keyMod&(kKeyShiftMask|kKeyControlMask));
439  //std::cout <<"rowClicked "<<row<<" "<<btn<<" "<<keyMod<<std::endl;
440  Long_t args[3];
441  args[0]=(Long_t)column;
442  args[1]=(Long_t)btn;
443  args[2]=(Long_t)keyMod;
444  Emit("columnClicked(Int_t,Int_t,Int_t)",args);
445 }
446 
447 void
449 {
450  bool needs_layout = m_forceLayout; m_forceLayout = false;
451 
452  m_body->dataChanged();
453  if(m_rowHeader) {
456  }
457  //set sizes
458  std::vector<unsigned int> columnWidths = m_body->widthOfTextInColumns();
459  if(m_header) {
460  // reset header back to its internal max rather than the last width
461  m_header->dataChanged();
462  std::vector<unsigned int> headerWidths = m_header->widthOfTextInColumns();
463  for(std::vector<unsigned int>::iterator it = columnWidths.begin(), itEnd=columnWidths.end(), itHeader=headerWidths.begin();
464  it != itEnd;
465  ++it,++itHeader) {
466  if(*itHeader > *it) {
467  *it = *itHeader;
468  }
469  }
470  m_header->setWidthOfTextInColumns(columnWidths);
471  }
472  m_body->setWidthOfTextInColumns(columnWidths);
473 
474  //this updates sliders to match our new data
475  bool layoutDoneByhandleResize = handleResize(GetWidth(), GetHeight());
476  if (needs_layout && ! layoutDoneByhandleResize)
477  {
478  Layout();
479  }
480  gClient->NeedRedraw(m_body);
481  if (m_header) gClient->NeedRedraw(m_header);
482  if (m_rowHeader) gClient->NeedRedraw(m_rowHeader);
483 
484 }
485 
486 void
487 FWTableWidget::buttonPressedInRowHeader(Int_t row, Int_t column, Event_t* event, Int_t relX, Int_t relY)
488 {
489  Int_t btn = event->fCode;
490  if(btn != kButton1 && btn != kButton3) {return;}
491  m_bodyTable->buttonReleasedInRowHeader(row, event, relX, relY);
492 }
493 void
494 FWTableWidget::buttonReleasedInRowHeader(Int_t row, Int_t column, Event_t* event, Int_t relX, Int_t relY)
495 {
496  Int_t btn = event->fCode;
497  if(btn != kButton1 && btn != kButton3) {return;}
498  m_bodyTable->buttonReleasedInRowHeader(row, event, relX, relY);
499 }
500 
501 //
502 // const member functions
503 //
504 TGDimension
506 {
507  TGDimension returnValue;
508  if(m_header){
509  returnValue.fHeight += m_header->GetDefaultHeight();
510  }
511  if(m_rowHeader) {
512  returnValue.fWidth += m_rowHeader->GetDefaultWidth();
513  }
514  returnValue = returnValue + m_body->GetDefaultSize();
515  returnValue.fHeight += m_hSlider->GetDefaultHeight();
516  returnValue.fWidth += m_vSlider->GetDefaultWidth();
517 
518  return returnValue;
519 }
520 
521 void
523 {
527 }
528 
529 void
531 {
532  // override virtual TGFrame::DoRedraw() to prevent call of gVirtualX->ClearArea();
533 }
534 //
535 // static member functions
536 //
537 
538 ClassImp(FWTableWidget)
void SetBackgroundColor(Pixel_t)
virtual bool cellDataIsSortable() const
void buttonReleasedInRowHeader(Int_t row, Int_t column, Event_t *event, Int_t relX, Int_t relY)
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
FWTableManagerBase * m_bodyTable
TGVScrollBar * m_vSlider
TGGC * m_headerBackground
void sort(int iCol, bool iSortOrder)
Call to have table sorted on values in column iCol with the sort order being descending if iSortOrder...
void SetHeaderBackgroundColor(Pixel_t)
void setLineContext(GContext_t iContext)
bool handleResize(UInt_t w, UInt_t h)
TGDimension GetDefaultSize() const
void SetLineSeparatorColor(Pixel_t)
static const unsigned long kWidgetColor
void buttonReleasedInBody(Int_t row, Int_t column, Event_t *event, Int_t, Int_t)
virtual ~FWTableWidget()
void sort(UInt_t iColumn, bool iDescendingSort)
void disableGrowInWidth()
void setWidthOfTextInColumns(const std::vector< unsigned int > &)
FWTabularWidget * m_body
virtual int unsortedRowNumber(int iSortedRowNumber) const =0
when passed the index to the sorted order of the rows it returns the original row number from the und...
TGGC * m_headerForeground
void cellClicked(Int_t iRow, Int_t iColumn, Int_t iButton, Int_t iKeyMod, Int_t iGlobalX, Int_t iGlobalY)
virtual void MoveResize(Int_t x, Int_t y, UInt_t w=0, UInt_t h=0)
TGDimension GetDefaultSize() const
TGGC * m_lineSeparator
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
virtual void buttonReleasedInRowHeader(Int_t row, Event_t *event, Int_t relX, Int_t relY)
virtual void DoRedraw()
void disableGrowInWidth()
void setHorizontalOffset(UInt_t)
const std::vector< unsigned int > & widthOfTextInColumns() const
unsigned int UInt_t
Definition: FUTypes.h:12
static const UInt_t kRowOptions
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
FWTabularWidget * m_rowHeader
void columnClicked(Int_t iColumn, Int_t iButton, Int_t iKeyMod)
void setBackgroundAreaContext(GContext_t iContext)
void buttonPressedInRowHeader(Int_t row, Int_t column, Event_t *event, Int_t relX, Int_t relY)
dictionary args
void childrenEvent(Event_t *)
void rowClicked(Int_t iRow, Int_t iButton, Int_t iKeyMod, Int_t iGlobalX, Int_t iGlobalY)
void SetHeaderForegroundColor(Pixel_t)
static const UInt_t kColOptions
virtual void Resize(UInt_t w, UInt_t h)
void buttonReleasedInHeader(Int_t row, Int_t column, Event_t *event, Int_t, Int_t)
Definition: DDAxes.h:10
FWTableManagerBase * m_headerTable
FWTabularWidget * m_header
FWTableManagerBase * m_rowHeaderTable
void setVerticalOffset(UInt_t)
JetCorrectorParameters::Definitions def
Definition: classes.h:10
TGHScrollBar * m_hSlider
T w() const
FWTableWidget(FWTableManagerBase *iManager, const TGWindow *p=0)