Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGTableCell.cxx
Go to the documentation of this file.
1 // Author: Roel Aaij 21/07/2007
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #include "TGFrame.h"
12 #include "TClass.h"
13 #include "TGWidget.h"
14 #include "TGWindow.h"
15 #include "TGResourcePool.h"
16 #include "Riostream.h"
17 #include "TSystem.h"
18 #include "TImage.h"
19 #include "TEnv.h"
20 #include "TGToolTip.h"
21 #include "TGPicture.h"
22 #include "TGTable.h"
23 #include "TVirtualTableInterface.h"
24 #include "TColor.h"
25 
26 ClassImp(TGTableCell);
27 
28 //////////////////////////////////////////////////////////////////////////
29 // //
30 // TGTableCell //
31 // //
32 // TGTableCell is the class that represents a single cell in a TGTable. //
33 // //
34 // This class is for internal use in TGTable only. //
35 // //
36 //////////////////////////////////////////////////////////////////////////
37 
38 const TGGC *TGTableCell::fgDefaultGC = 0;
39 const TGFont *TGTableCell::fgDefaultFont = 0;
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// TGTableCell constructor.
43 
44 TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, TGString *label,
45  UInt_t row, UInt_t column, UInt_t width, UInt_t height,
46  GContext_t norm, FontStruct_t font, UInt_t option,
47  Bool_t resize)
48  : TGFrame(p, width, height, option), fTip(0), fReadOnly(kFALSE),
49  fEnabled(kTRUE), fTMode(kTextRight | kTextCenterY), fImage(0),
50  fFontStruct(font), fHasOwnFont(kFALSE), fColumn(column), fRow(row),
51  fTable(table)
52 {
53  if (label) {
54  fLabel = label;
55  } else {
56  fLabel = new TGString("0");
57  }
58 
59  fNormGC = norm;
60  Init(resize);
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// TGTableCell constructor
65 
66 TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, const char *label,
67  UInt_t row, UInt_t column, UInt_t width, UInt_t height,
68  GContext_t norm, FontStruct_t font, UInt_t option,
69  Bool_t resize)
70  : TGFrame(p, width, height, option), fTip(0), fReadOnly(kFALSE),
71  fEnabled(kTRUE), fTMode(kTextRight | kTextCenterY), fImage(0),
72  fFontStruct(font), fHasOwnFont(kFALSE), fColumn(column), fRow(row),
73  fTable(table)
74 {
75  if (label) {
76  fLabel = new TGString(label);
77  } else {
78  fLabel = new TGString("0");
79  }
80 
81  fNormGC = norm;
82  Init(resize);
83 }
84 
85 // //______________________________________________________________________________
86 // TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, TGPicture *image,
87 // TGString *label, UInt_t row, UInt_t column,
88 // GContext_t norm, FontStruct_t font, UInt_t option,
89 // Bool_t resize)
90 // : TGFrame(p, 80, 25, option), fTip(0), fReadOnly(kFALSE), fEnabled(kTRUE),
91 // fTMode(kTextRight | kTextCenterY), fImage(image), fFontStruct(font),
92 // fHasOwnFont(kFALSE), fColumn(column), fRow(row), fTable(table)
93 // {
94 // if (label) {
95 // fLabel = label;
96 // } else {
97 // fLabel = new TGString("0");
98 // }
99 // fNormGC = norm;
100 
101 // Init(resize);
102 // }
103 
104 // //______________________________________________________________________________
105 // TGTableCell::TGTableCell(const TGWindow *p, TGTable *table, TGPicture *image,
106 // const char *label, UInt_t row, UInt_t column,
107 // GContext_t norm, FontStruct_t font, UInt_t option,
108 // Bool_t resize)
109 // : TGFrame(p, 80, 25, option), fTip(0), fReadOnly(kFALSE), fEnabled(kTRUE),
110 // fTMode(kTextRight | kTextCenterY), fImage(image), fFontStruct(font),
111 // fHasOwnFont(kFALSE), fColumn(column), fRow(row), fTable(table)
112 // {
113 
114 // if (label) {
115 // fLabel = new TGString(label);
116 // } else {
117 // fLabel = new TGString("0");
118 // }
119 
120 // fNormGC = norm;
121 
122 // Init(resize);
123 // }
124 
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// TGTableCell destructor.
128 
129 TGTableCell::~TGTableCell()
130 {
131  if (fImage) delete fImage;
132  if (fLabel) delete fLabel;
133  if (fTip) delete fTip;
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Initialise the TGTableCell.
138 
139 void TGTableCell::Init(Bool_t resize)
140 {
141  Int_t max_ascent = 0, max_descent = 0;
142 
143  fTWidth = gVirtualX->TextWidth(fFontStruct, fLabel->GetString(), fLabel->GetLength());
144  gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
145  fTHeight = max_ascent + max_descent;
146 
147  // Modifications for individual cell drawing test, original block is marked
148 
149  if (fTable) {
150  // Original from here
151  TGTableHeader *chdr = 0;
152  TGTableHeader *rhdr = 0;
153  if(resize) {
154  chdr = fTable->GetColumnHeader(fColumn);
155  rhdr = fTable->GetRowHeader(fRow);
156  if (rhdr) {
157  SetBackgroundColor(rhdr->GetBackground());
158  if (chdr) Resize(chdr->GetWidth(), rhdr->GetHeight());
159  }
160  }
161  SetBackgroundColor(fTable->GetRowBackground(fRow));
162  // Upto here
163  } else {
164  fWidth = 80;
165  fHeight = 25;
166  Resize(fWidth, fHeight);
167  SetBackgroundColor(fgWhitePixel);
168  }
169  // End of modifications
170 
171 }
172 
173 ////////////////////////////////////////////////////////////////////////////////
174 /// Redraw the TGTableCell.
175 
176 void TGTableCell::DoRedraw()
177 {
178  TGFrame::DoRedraw();
179 
180  Int_t x = 0, y = 0;
181 
182  // To be done: Add a tooltip with the complete label when it
183  // doesn't fit in the cell.
184  if (fTWidth > fWidth - 4) fTMode = kTextLeft;
185 
186  if (fTMode & kTextLeft) {
187  x = 4;
188  } else if (fTMode & kTextRight) {
189  x = fWidth - fTWidth - 4;
190  } else {
191  x = (fWidth - fTWidth) / 2;
192  }
193 
194  if (fTMode & kTextTop) {
195  y = 3;
196  } else if (fTMode & kTextBottom) {
197  y = fHeight - fTHeight - 3;
198  } else {
199  y = (fHeight - fTHeight - 4) / 2;
200  }
201 
202  y += fTHeight;
203 
204  fLabel->Draw(fId, fNormGC, x, y);
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Move the TGTableCell and redraw it.
209 
210 void TGTableCell::MoveDraw(Int_t x, Int_t y)
211 {
212  // Note, this method is unused.
213 
214  TGFrame::Move(x, y);
215  DoRedraw();
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// Resize the TGTableCell.
220 
221 void TGTableCell::Resize(UInt_t width, UInt_t height)
222 {
223  fWidth = width;
224  fHeight = height;
225  TGFrame::Resize(width, height);
226  Layout();
227 }
228 
229 ////////////////////////////////////////////////////////////////////////////////
230 /// Resize the TGTableCell.
231 
232 void TGTableCell::Resize(TGDimension newsize)
233 {
234  Resize(newsize.fWidth, newsize.fHeight);
235 }
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 /// Return default font structure.
239 
240 FontStruct_t TGTableCell::GetDefaultFontStruct()
241 {
242  if (!fgDefaultFont)
243  fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
244  return fgDefaultFont->GetFontStruct();
245 }
246 
247 ////////////////////////////////////////////////////////////////////////////////
248 /// Return default graphics context.
249 
250 const TGGC &TGTableCell::GetDefaultGC()
251 {
252  if (!fgDefaultGC)
253  fgDefaultGC = gClient->GetResourcePool()->GetFrameGC();
254  return *fgDefaultGC;
255 }
256 
257 ////////////////////////////////////////////////////////////////////////////////
258 /// Draw the cell border.
259 
260 void TGTableCell::DrawBorder()
261 {
262  // FIXME Borders are drawn very crudely. There is much room for improvement.
263  gVirtualX->DrawRectangle(fId, fNormGC, 0, 0, fWidth - 1, fHeight - 1);
264 }
265 
266 ////////////////////////////////////////////////////////////////////////////////
267 /// DrawBorder called from DrawCopy.
268 
269 void TGTableCell::DrawBorder(Handle_t id, Int_t x, Int_t y)
270 {
271  gVirtualX->DrawRectangle(id, fNormGC, x, y, x + fWidth - 1, y +fHeight - 1);
272 }
273 
274 ////////////////////////////////////////////////////////////////////////////////
275 /// Highlight the this TGTableCell.
276 
277 void TGTableCell::Highlight()
278 {
279  // Currently not implemented.
280 }
281 
282 // //______________________________________________________________________________
283 // void TGTableCell::SetRow(UInt_t row)
284 // {
285 // fRow = row;
286 // }
287 
288 // //______________________________________________________________________________
289 // void TGTableCell::SetColumn(UInt_t column)
290 // {
291 // fColumn = column;
292 // }
293 
294 ////////////////////////////////////////////////////////////////////////////////
295 /// Draw list view item in other window.
296 /// List view item is placed and layout in the container frame,
297 /// but is drawn in viewport.
298 
299 void TGTableCell::DrawCopy(Handle_t id, Int_t x, Int_t y)
300 {
301  // FIXME this method is only needed if the table container frame is a
302  // TGContainer. It is broken and not used in the current implementation.
303 
304  Int_t lx = 0, ly = 0;
305 
306  if (fTMode & kTextLeft) {
307  lx = 4;
308  } else if (fTMode & kTextRight) {
309  lx = fWidth - fTWidth - 4;
310  } else {
311  lx = (fWidth - fTWidth) / 2;
312  }
313 
314  if (fTMode & kTextTop) {
315  ly = 3;
316  } else if (fTMode & kTextBottom) {
317  ly = fHeight - fTHeight - 3;
318  } else {
319  ly = (fHeight - fTHeight - 4) / 2;
320  }
321 
322  ly += fTHeight;
323 
324  // if (fActive) {
325  // gVirtualX->SetForeground(fNormGC, fgDefaultSelectedBackground);
326  // gVirtualX->FillRectangle(id, fNormGC, x + lx, y + ly, fTWidth, fTHeight + 1);
327  // gVirtualX->SetForeground(fNormGC, fClient->GetResourcePool()->GetSelectedFgndColor());
328  // } else {
329 
330  gVirtualX->SetForeground(fNormGC, fgWhitePixel);
331  gVirtualX->FillRectangle(id, fNormGC, x, y, fWidth, fHeight);
332  gVirtualX->SetForeground(fNormGC, fgBlackPixel);
333  DrawBorder(id, x, y);
334 
335  // }
336 
337  fLabel->Draw(id, fNormGC, x + lx, y + ly);
338 }
339 
340 ////////////////////////////////////////////////////////////////////////////////
341 /// Set the label of this cell to label.
342 
343 void TGTableCell::SetLabel(const char *label)
344 {
345  fLabel->SetString(label);
346 
347  Int_t max_ascent = 0, max_descent = 0;
348 
349  fTWidth = gVirtualX->TextWidth(fFontStruct, fLabel->GetString(), fLabel->GetLength());
350  gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
351  fTHeight = max_ascent + max_descent;
352 
353 }
354 
355 ////////////////////////////////////////////////////////////////////////////////
356 /// Set the image that this cell contains to image.
357 
358 void TGTableCell::SetImage(TGPicture *image)
359 {
360  // Note: currently not used.
361  if (fImage) delete fImage;
362  fImage = image;
363 }
364 
365 // //______________________________________________________________________________
366 // void TGTableCell::SetBckgndGC(TGGC *gc)
367 // {
368 // }
369 
370 ////////////////////////////////////////////////////////////////////////////////
371 /// Changes text font.
372 /// If global is kTRUE font is changed globally, otherwise - locally.
373 
374 void TGTableCell::SetFont(FontStruct_t font)
375 {
376  if (font != fFontStruct) {
377  FontH_t v = gVirtualX->GetFontHandle(font);
378  if (!v) return;
379 
380  fFontStruct = font;
381  TGGCPool *pool = fClient->GetResourcePool()->GetGCPool();
382  TGGC *gc = pool->FindGC(fNormGC);
383 
384  gc = pool->GetGC((GCValues_t*)gc->GetAttributes(), kTRUE); // copy
385  fHasOwnFont = kTRUE;
386 
387  gc->SetFont(v);
388 
389  fNormGC = gc->GetGC();
390  gClient->NeedRedraw(this);
391  }
392 }
393 
394 ////////////////////////////////////////////////////////////////////////////////
395 /// Changes text font specified by name.
396 /// If global is true color is changed globally, otherwise - locally.
397 
398 void TGTableCell::SetFont(const char *fontName)
399 {
400  TGFont *font = fClient->GetFont(fontName);
401  if (font) {
402  SetFont(font->GetFontStruct());
403  }
404 }
405 
406 ////////////////////////////////////////////////////////////////////////////////
407 /// Set the text justify mode of the cell to mode.
408 
409 void TGTableCell::SetTextJustify(Int_t tmode)
410 {
411  fTMode = tmode;
412 }
413 
414 ////////////////////////////////////////////////////////////////////////////////
415 /// Select this TGTableCell.
416 
417 void TGTableCell::Select()
418 {
419  // Note: currently not implemented.
420 }
421 
422 ////////////////////////////////////////////////////////////////////////////////
423 /// Select the row that this TGTableCell belongs to.
424 
425 void TGTableCell::SelectRow()
426 {
427  // Note: currently not implemented.
428 }
429 
430 ////////////////////////////////////////////////////////////////////////////////
431 /// Select the column that this TGTableCell belongs to.
432 
433 void TGTableCell::SelectColumn()
434 {
435  // Note: currently not implemented.
436 }