Logo ROOT   6.08/07
Reference Guide
TGPicture.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 01/01/98
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 /**************************************************************************
12 
13  This source is based on Xclass95, a Win95-looking GUI toolkit.
14  Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
15 
16  Xclass95 is free software; you can redistribute it and/or
17  modify it under the terms of the GNU Library General Public
18  License as published by the Free Software Foundation; either
19  version 2 of the License, or (at your option) any later version.
20 
21 **************************************************************************/
22 
23 //////////////////////////////////////////////////////////////////////////
24 // //
25 // TGPicture & TGPicturePool //
26 // //
27 // The TGPicture class implements pictures and icons used in the //
28 // different GUI elements and widgets. The TGPicturePool class //
29 // implements a TGPicture cache. TGPictures are created, managed and //
30 // destroyed by the TGPicturePool. //
31 // //
32 //////////////////////////////////////////////////////////////////////////
33 
34 #include "TGPicture.h"
35 #include "TGResourcePool.h"
36 #include "THashTable.h"
37 #include "TSystem.h"
38 #include "TGWindow.h"
39 #include "TVirtualX.h"
40 #include "TImage.h"
41 #include <stdlib.h>
42 
44 
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 ///copy constructor
52 
54  TObject(pp),
55  fClient(pp.fClient),
56  fPath(pp.fPath),
57  fPicList(pp.fPicList)
58 {
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 ///assignment operator
63 
65 {
66  if(this!=&pp) {
68  fClient=pp.fClient;
69  fPath=pp.fPath;
70  fPicList=pp.fPicList;
71  }
72  return *this;
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Get a picture from the picture pool. Picture must be freed using
77 /// TGPicturePool::FreePicture(). If picture is not found 0 is returned.
78 
80 {
81  if (!fPicList)
82  fPicList = new THashTable(50);
83 
84  TString pname = name;
85  pname.Strip();
86  TString ext = strrchr(pname, '.');
87  ext.ToLower();
88 
89  if (ext.Length()) { // ".xpm", ".gif" etc
90  char *pxname = gSystem->ExpandPathName(gSystem->UnixPathName(pname));
91  pname = pxname;
92  delete [] pxname;
93  }
94 
95  TGPicture *pic = (TGPicture *)fPicList->FindObject(pname);
96  if (pic && !pic->IsScaled()) {
97  if (pic->fPic == kNone)
98  return 0;
99  pic->AddReference();
100  return pic;
101  }
102 
103  char *picnam = gSystem->Which(fPath, pname, kReadPermission);
104  if (!picnam) {
105  pic = new TGPicture(pname);
107  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
109  fPicList->Add(pic);
110  return 0;
111  }
112 
113  TImage *img = TImage::Open(picnam);
114  if (!img) {
115  pic = new TGPicture(pname);
117  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
119  fPicList->Add(pic);
120  delete [] picnam;
121  return 0;
122  }
123 
124  pic = new TGPicture(pname, img->GetPixmap(), img->GetMask());
125  delete [] picnam;
126  delete img;
127  fPicList->Add(pic);
128  return pic;
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Get picture with specified size from pool (picture will be scaled if
133 /// necessary). Picture must be freed using TGPicturePool::FreePicture(). If
134 /// picture is not found 0 is returned.
135 
137  UInt_t new_width, UInt_t new_height)
138 {
139  if (!fPicList)
140  fPicList = new THashTable(50);
141 
142  TString pname = name;
143  pname.Strip();
144  TString ext = strrchr(pname, '.');
145  ext.ToLower();
146 
147  if (ext.Length()) { // ".xpm", ".gif" etc
148  char *pxname = gSystem->ExpandPathName(gSystem->UnixPathName(pname));
149  pname = pxname;
150  delete [] pxname;
151  }
152 
153  const char *hname = TGPicture::HashName(pname, new_width, new_height);
154  TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
155  if (pic && pic->GetWidth() == new_width && pic->GetHeight() == new_height) {
156  if (pic->fPic == kNone)
157  return 0;
158  pic->AddReference();
159  return pic;
160  }
161 
162  char *picnam = gSystem->Which(fPath, pname, kReadPermission);
163  if (!picnam) {
164  pic = new TGPicture(hname, kTRUE);
166  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
168  pic->fAttributes.fWidth = new_width;
169  pic->fAttributes.fHeight = new_height;
170  fPicList->Add(pic);
171  return 0;
172  }
173 
174  TImage *img = TImage::Open(picnam);
175  if (!img) {
176  pic = new TGPicture(hname, kTRUE);
178  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
180  pic->fAttributes.fWidth = new_width;
181  pic->fAttributes.fHeight = new_height;
182  fPicList->Add(pic);
183  delete [] picnam;
184  return 0;
185  }
186 
187  img->Scale(new_width, new_height);
188 
189  pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
190  delete [] picnam;
191  delete img;
192  fPicList->Add(pic);
193  return pic;
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Get picture with specified pixmap and mask from pool.
198 /// Picture must be freed using TGPicturePool::FreePicture().
199 /// If picture is not found 0 is returned.
200 
202  Pixmap_t mask)
203 {
204  if (!fPicList)
205  fPicList = new THashTable(50);
206 
207  Int_t xy;
208  UInt_t w, h;
209 
210  gVirtualX->GetWindowSize(pxmap, xy, xy, w, h);
211 
212  const char *hname = TGPicture::HashName(name, w, h);
213  TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
214 
215  if (pic) {
216  pic->AddReference();
217  return pic;
218  }
219 
220  pic = new TGPicture(hname, pxmap, mask);
221  fPicList->Add(pic);
222 
223  return pic;
224 }
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Create picture from XPM data.
228 /// Picture must be freed using TGPicturePool::FreePicture().
229 /// If picture creation failed 0 is returned.
230 
231 const TGPicture *TGPicturePool::GetPicture(const char *name, char **xpm)
232 {
233  UInt_t w, h;
234 
235  if (!xpm || !*xpm) {
236  return 0;
237  }
238 
239  if (!fPicList) {
240  fPicList = new THashTable(50);
241  }
242  char *ptr = xpm[0];
243  while (isspace((int)*ptr)) ++ptr;
244  w = atoi(ptr);
245 
246  while (isspace((int)*ptr)) ++ptr;
247  h = atoi(ptr);
248 
249  const char *hname = TGPicture::HashName(name, w, h);
250  TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
251  if (pic) {
252  pic->AddReference();
253  return pic;
254  }
255 
256  TImage *img = TImage::Open(xpm);
257  if (!img) {
258  pic = new TGPicture(hname, kTRUE);
260  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
262  pic->fAttributes.fWidth = w;
263  pic->fAttributes.fHeight = h;
264  fPicList->Add(pic);
265  return 0;
266  }
267 
268  pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
269  delete img;
270  return pic;
271 }
272 
273 ////////////////////////////////////////////////////////////////////////////////
274 /// Remove picture from cache if nobody is using it anymore.
275 
277 {
278  if (!fPicList) return;
279 
280  TGPicture *pic = (TGPicture *)fPicList->FindObject(fpic);
281  if (pic) {
282  if (pic->RemoveReference() == 0) {
283  fPicList->Remove(pic);
284  delete pic;
285  }
286  }
287 }
288 
289 ////////////////////////////////////////////////////////////////////////////////
290 /// Delete picture cache.
291 
293 {
294  if (fPicList) {
295  fPicList->Delete();
296  delete fPicList;
297  }
298 }
299 
300 ////////////////////////////////////////////////////////////////////////////////
301 /// List all pictures in the pool.
302 
304 {
305  if (fPicList)
306  fPicList->Print();
307  else
308  Info("Print", "no pictures in picture pool");
309 }
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// ctor. Important: both pixmaps pxmap and mask must be unique (not shared)
313 
314 TGPicture::TGPicture(const char *name, Pixmap_t pxmap, Pixmap_t mask)
315 {
316  fName = name;
317  fScaled = kFALSE;
318  fPic = pxmap;
319  fMask = mask;
320  Int_t xy;
321 
322  fAttributes.fColormap = gClient->GetDefaultColormap();
323  fAttributes.fCloseness = 40000; // Allow for "similar" colors
324  fAttributes.fMask = kPASize | kPAColormap | kPACloseness;
325  fAttributes.fPixels = 0;
326  fAttributes.fDepth = 0;
327  fAttributes.fNpixels = 0;
328  fAttributes.fXHotspot = 0;
329  fAttributes.fYHotspot = 0;
330 
331  gVirtualX->GetWindowSize(fPic, xy, xy, fAttributes.fWidth, fAttributes.fHeight);
332  SetRefCount(1);
333 }
334 
335 ////////////////////////////////////////////////////////////////////////////////
336 /// Draw a picture.
337 
339 {
340  GCValues_t gcv;
341 
343  gcv.fClipMask = fMask;
344  gcv.fClipXOrigin = x;
345  gcv.fClipYOrigin = y;
346  gVirtualX->ChangeGC(gc, &gcv);
347  gVirtualX->CopyArea(fPic, id, gc, 0, 0, fAttributes.fWidth, fAttributes.fHeight,
348  x, y);
349  gcv.fMask = kGCClipMask;
350  gcv.fClipMask = kNone;
351  gVirtualX->ChangeGC(gc, &gcv);
352 }
353 
354 ////////////////////////////////////////////////////////////////////////////////
355 /// Delete picture object.
356 
358 {
359  if (fPic != kNone)
360  gVirtualX->DeletePixmap(fPic);
361  if (fMask != kNone)
362  gVirtualX->DeletePixmap(fMask);
363  if (fAttributes.fPixels)
364  delete [] fAttributes.fPixels;
365 }
366 
367 ////////////////////////////////////////////////////////////////////////////////
368 /// Static function returning a unique name used to look up a picture.
369 /// The unique name has the form "name__widthxheight".
370 
371 const char *TGPicture::HashName(const char *name, Int_t width, Int_t height)
372 {
373  static TString hashName;
374 
375  hashName.Form("%s__%dx%d", name, width, height);
376  return hashName.Data();
377 }
378 
379 ////////////////////////////////////////////////////////////////////////////////
380 /// Print picture info.
381 
383 {
384  Printf("TGPicture: %s,%sref cnt = %u %lx", GetName(),
385  fScaled ? " scaled, " : " ", References(), fPic);
386 }
387 
388 
389 ////////////////////////////////////////////////////////////////////////////////
390 /// Create a "selected" looking picture based on the original TGPicture.
391 
393  TGPicture("")
394 {
395  GCValues_t gcv;
396  UInt_t w, h;
397 
398  fClient = client;
399  Window_t root = fClient->GetDefaultRoot()->GetId();
400 
401  w = p->GetWidth();
402  h = p->GetHeight();
403 
404  fPic = gVirtualX->CreatePixmap(root, w, h);
405  fMask = p->GetMask();
406 
407  fAttributes.fWidth = w;
409 
410  gVirtualX->CopyArea(p->GetPicture(), fPic, GetSelectedGC()(), 0, 0, w, h, 0, 0);
411 
413  gcv.fClipMask = p->GetMask();
414  gcv.fClipXOrigin = 0;
415  gcv.fClipYOrigin = 0;
417 
418  gVirtualX->FillRectangle(fPic, GetSelectedGC()(), 0, 0, w, h);
419 
421 }
422 
423 ////////////////////////////////////////////////////////////////////////////////
424 /// Delete selected picture.
425 
427 {
428  // fMask was borrowed so should not be deleted by ~TGPicture.
429  fMask = kNone;
430 }
431 
432 ////////////////////////////////////////////////////////////////////////////////
433 /// Return selection graphics context in use.
434 
436 {
437  if (!fgSelectedGC) {
438  fgSelectedGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
439  fgSelectedGC->SetForeground(gClient->GetResourcePool()->GetSelectedBgndColor());
440  fgSelectedGC->SetBackground(gClient->GetResourcePool()->GetBlackColor());
442  fgSelectedGC->SetStipple(gClient->GetResourcePool()->GetCheckeredBitmap());
443  }
444  return *fgSelectedGC;
445 }
Int_t fClipXOrigin
Definition: GuiTypes.h:246
void Print(Option_t *option="") const
List all pictures in the pool.
Definition: TGPicture.cxx:303
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:899
static const char * HashName(const char *name, Int_t width, Int_t height)
Static function returning a unique name used to look up a picture.
Definition: TGPicture.cxx:371
const char Option_t
Definition: RtypesCore.h:62
void SetForeground(Pixel_t v)
Set foreground color.
Definition: TGGC.cxx:276
TH1 * h
Definition: legend2.C:5
Handle_t GContext_t
Definition: GuiTypes.h:39
Bool_t IsScaled() const
Definition: TGPicture.h:77
Basic string class.
Definition: TString.h:137
#define gClient
Definition: TGClient.h:174
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1089
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition: TSystem.cxx:1512
PictureAttributes_t fAttributes
Definition: TGPicture.h:55
Int_t fClipYOrigin
Definition: GuiTypes.h:247
An abstract interface to image processing library.
Definition: TImage.h:37
const TGClient * fClient
Definition: TGPicture.h:113
Pixmap_t fClipMask
Definition: GuiTypes.h:248
UInt_t GetHeight() const
Definition: TGPicture.h:74
virtual ~TGSelectedPicture()
Delete selected picture.
Definition: TGPicture.cxx:426
Handle_t GetId() const
Definition: TGObject.h:52
virtual Pixmap_t GetPixmap()
Definition: TImage.h:243
TGSelectedPicture(const TGSelectedPicture &gp)
Definition: TGPicture.h:96
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1037
virtual void Scale(UInt_t, UInt_t)
Definition: TImage.h:149
void Draw(Option_t *="")
Default Draw method for all objects.
Definition: TGPicture.h:67
void SetBackground(Pixel_t v)
Set background color.
Definition: TGGC.cxx:287
const Mask_t kGCClipMask
Definition: GuiTypes.h:306
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:39
Double_t x[n]
Definition: legend1.C:17
static TGGC * fgSelectedGC
Definition: TGPicture.h:93
const Mask_t kPASize
Definition: GuiTypes.h:340
void FreePicture(const TGPicture *pic)
Remove picture from cache if nobody is using it anymore.
Definition: TGPicture.cxx:276
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.cxx:103
void SetStipple(Pixmap_t v)
Set 1 plane pixmap for stippling.
Definition: TGGC.cxx:376
TString fPath
Definition: TGPicture.h:114
const Mask_t kPAColormap
Definition: GuiTypes.h:338
Pixmap_t GetPicture() const
Definition: TGPicture.h:75
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
XPoint xy[kMAXMK]
Definition: TGX11.cxx:122
const Mask_t kGCClipXOrigin
Definition: GuiTypes.h:304
void SetFillStyle(Int_t v)
Set fill style (kFillSolid, kFillTiled, kFillStippled, kFillOpaeueStippled).
Definition: TGGC.cxx:343
virtual ~TGPicture()
Delete picture object.
Definition: TGPicture.cxx:357
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2322
unsigned int UInt_t
Definition: RtypesCore.h:42
const Handle_t kNone
Definition: GuiTypes.h:89
Ssiz_t Length() const
Definition: TString.h:390
const TGPicture * GetPicture(const char *name)
Get a picture from the picture pool.
Definition: TGPicture.cxx:79
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1070
const Mask_t kGCClipYOrigin
Definition: GuiTypes.h:305
#define gVirtualX
Definition: TVirtualX.h:362
#define Printf
Definition: TGeoToOCC.h:18
static TGGC & GetSelectedGC()
Return selection graphics context in use.
Definition: TGPicture.cxx:435
THashTable * fPicList
Definition: TGPicture.h:115
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:210
#define ClassImp(name)
Definition: Rtypes.h:279
virtual ~TGPicturePool()
Delete picture cache.
Definition: TGPicture.cxx:292
virtual Pixmap_t GetMask()
Definition: TImage.h:244
UInt_t RemoveReference()
Definition: TRefCnt.h:43
Pixmap_t fMask
Definition: TGPicture.h:54
void Print(Option_t *option="") const
Print picture info.
Definition: TGPicture.cxx:382
TGPicture(const char *name, Bool_t scaled=kFALSE)
Definition: TGPicture.h:57
Double_t y[n]
Definition: legend1.C:17
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:76
UInt_t GetWidth() const
Definition: TGPicture.h:73
void SetAttributes(GCValues_t *values)
Set attributes as specified in the values structure.
Definition: TGGC.cxx:233
Mother of all ROOT objects.
Definition: TObject.h:37
Colormap_t GetDefaultColormap() const
Definition: TGClient.h:162
void Delete(Option_t *option="")
Remove all objects from the table AND delete all heap based objects.
Definition: THashTable.cxx:194
Handle_t Window_t
Definition: GuiTypes.h:30
Mask_t fMask
Definition: GuiTypes.h:252
void AddReference()
Definition: TRefCnt.h:42
Colormap_t fColormap
Definition: GuiTypes.h:325
Handle_t Pixmap_t
Definition: GuiTypes.h:31
const TGWindow * GetDefaultRoot() const
Returns the root (i.e.
Definition: TGClient.cxx:232
Pixmap_t GetMask() const
Definition: TGPicture.h:76
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition: TImage.cxx:110
TGPicturePool & operator=(const TGPicturePool &)
assignment operator
Definition: TGPicture.cxx:64
const Mask_t kPACloseness
Definition: GuiTypes.h:343
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1244
Pixmap_t fPic
Definition: TGPicture.h:53
virtual void Print(Option_t *option="") const
Default print for collections, calls Print(option, 1).
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:416
const Bool_t kTRUE
Definition: Rtypes.h:91
void SetClipMask(Pixmap_t v)
Bitmap for clipping.
Definition: TGGC.cxx:464
Definition: TGGC.h:35
char name[80]
Definition: TGX11.cxx:109
const TGClient * fClient
Definition: TGPicture.h:91
TObject * Remove(TObject *obj)
Remove object from the hashtable.
Definition: THashTable.cxx:315
const char * Data() const
Definition: TString.h:349
ULong_t Handle_t
Definition: GuiTypes.h:27