Logo ROOT   master
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 "TROOT.h"
42 #include <stdlib.h>
43 
45 
49 
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 ///copy constructor
53 
55  TObject(pp),
56  fClient(pp.fClient),
57  fPath(pp.fPath),
58  fPicList(pp.fPicList)
59 {
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 ///assignment operator
64 
66 {
67  if(this!=&pp) {
69  fClient=pp.fClient;
70  fPath=pp.fPath;
71  fPicList=pp.fPicList;
72  }
73  return *this;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Get a picture from the picture pool. Picture must be freed using
78 /// TGPicturePool::FreePicture(). If picture is not found 0 is returned.
79 
81 {
82  if (!fPicList)
83  fPicList = new THashTable(50);
84 
85  TString pname = name;
86  pname.Strip();
87  TString ext = strrchr(pname, '.');
88  ext.ToLower();
89 
90  if (ext.Length()) { // ".xpm", ".gif" etc
91  pname = gSystem->UnixPathName(pname);
92  gSystem->ExpandPathName(pname);
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  pname = gSystem->UnixPathName(pname);
149  gSystem->ExpandPathName(pname);
150  }
151 
152  const char *hname = TGPicture::HashName(pname, new_width, new_height);
153  TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
154  if (pic && pic->GetWidth() == new_width && pic->GetHeight() == new_height) {
155  if (pic->fPic == kNone)
156  return 0;
157  pic->AddReference();
158  return pic;
159  }
160 
161  char *picnam = gSystem->Which(fPath, pname, kReadPermission);
162  if (!picnam) {
163  pic = new TGPicture(hname, kTRUE);
165  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
167  pic->fAttributes.fWidth = new_width;
168  pic->fAttributes.fHeight = new_height;
169  fPicList->Add(pic);
170  return 0;
171  }
172 
173  TImage *img = TImage::Open(picnam);
174  if (!img) {
175  pic = new TGPicture(hname, kTRUE);
177  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
179  pic->fAttributes.fWidth = new_width;
180  pic->fAttributes.fHeight = new_height;
181  fPicList->Add(pic);
182  delete [] picnam;
183  return 0;
184  }
185 
186  img->Scale(new_width, new_height);
187 
188  pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
189  delete [] picnam;
190  delete img;
191  fPicList->Add(pic);
192  return pic;
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Get picture with specified pixmap and mask from pool.
197 /// Picture must be freed using TGPicturePool::FreePicture().
198 /// If picture is not found 0 is returned.
199 
201  Pixmap_t mask)
202 {
203  if (!fPicList)
204  fPicList = new THashTable(50);
205 
206  Int_t xy;
207  UInt_t w, h;
208 
209  gVirtualX->GetWindowSize(pxmap, xy, xy, w, h);
210 
211  const char *hname = TGPicture::HashName(name, w, h);
212  TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
213 
214  if (pic) {
215  pic->AddReference();
216  return pic;
217  }
218 
219  pic = new TGPicture(hname, pxmap, mask);
220  fPicList->Add(pic);
221 
222  return pic;
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Create picture from XPM data.
227 /// Picture must be freed using TGPicturePool::FreePicture().
228 /// If picture creation failed 0 is returned.
229 
230 const TGPicture *TGPicturePool::GetPicture(const char *name, char **xpm)
231 {
232  UInt_t w, h;
233 
234  if (!xpm || !*xpm) {
235  return 0;
236  }
237 
238  if (!fPicList) {
239  fPicList = new THashTable(50);
240  }
241  char *ptr = xpm[0];
242  while (isspace((int)*ptr)) ++ptr;
243  w = atoi(ptr);
244 
245  while (isspace((int)*ptr)) ++ptr;
246  h = atoi(ptr);
247 
248  const char *hname = TGPicture::HashName(name, w, h);
249  TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
250  if (pic) {
251  pic->AddReference();
252  return pic;
253  }
254 
255  TImage *img = TImage::Open(xpm);
256  if (!img) {
257  pic = new TGPicture(hname, kTRUE);
259  pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
261  pic->fAttributes.fWidth = w;
262  pic->fAttributes.fHeight = h;
263  fPicList->Add(pic);
264  return 0;
265  }
266 
267  pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
268  delete img;
269  return pic;
270 }
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 /// Remove picture from cache if nobody is using it anymore.
274 
276 {
277  if (!fPicList) return;
278 
279  TGPicture *pic = (TGPicture *)fPicList->FindObject(fpic);
280  if (pic) {
281  if (pic->RemoveReference() == 0) {
282  fPicList->Remove(pic);
283  delete pic;
284  }
285  }
286 }
287 
288 ////////////////////////////////////////////////////////////////////////////////
289 /// Delete picture cache.
290 
292 {
293  if (fPicList) {
294  fPicList->Delete();
295  delete fPicList;
296  }
297 
298  // Required since we overload TObject::Hash.
300 }
301 
302 ////////////////////////////////////////////////////////////////////////////////
303 /// List all pictures in the pool.
304 
306 {
307  if (fPicList)
308  fPicList->Print();
309  else
310  Info("Print", "no pictures in picture pool");
311 }
312 
313 ////////////////////////////////////////////////////////////////////////////////
314 /// ctor. Important: both pixmaps pxmap and mask must be unique (not shared)
315 
316 TGPicture::TGPicture(const char *name, Pixmap_t pxmap, Pixmap_t mask)
317 {
318  fName = name;
319  fScaled = kFALSE;
320  fPic = pxmap;
321  fMask = mask;
322  Int_t xy;
323 
324  fAttributes.fColormap = gClient->GetDefaultColormap();
325  fAttributes.fCloseness = 40000; // Allow for "similar" colors
327  fAttributes.fPixels = 0;
328  fAttributes.fDepth = 0;
329  fAttributes.fNpixels = 0;
332 
333  gVirtualX->GetWindowSize(fPic, xy, xy, fAttributes.fWidth, fAttributes.fHeight);
334  SetRefCount(1);
335 }
336 
337 ////////////////////////////////////////////////////////////////////////////////
338 /// Draw a picture.
339 
341 {
342  GCValues_t gcv;
343 
345  gcv.fClipMask = fMask;
346  gcv.fClipXOrigin = x;
347  gcv.fClipYOrigin = y;
348  gVirtualX->ChangeGC(gc, &gcv);
349  gVirtualX->CopyArea(fPic, id, gc, 0, 0, fAttributes.fWidth, fAttributes.fHeight,
350  x, y);
351  gcv.fMask = kGCClipMask;
352  gcv.fClipMask = kNone;
353  gVirtualX->ChangeGC(gc, &gcv);
354 }
355 
356 ////////////////////////////////////////////////////////////////////////////////
357 /// Delete picture object.
358 
360 {
361  if (fPic != kNone)
362  gVirtualX->DeletePixmap(fPic);
363  if (fMask != kNone)
364  gVirtualX->DeletePixmap(fMask);
365  if (fAttributes.fPixels)
366  delete [] fAttributes.fPixels;
367 }
368 
369 ////////////////////////////////////////////////////////////////////////////////
370 /// Static function returning a unique name used to look up a picture.
371 /// The unique name has the form "name__widthxheight".
372 
373 const char *TGPicture::HashName(const char *name, Int_t width, Int_t height)
374 {
375  static TString hashName;
376 
377  hashName.Form("%s__%dx%d", name, width, height);
378  return hashName.Data();
379 }
380 
381 ////////////////////////////////////////////////////////////////////////////////
382 /// Print picture info.
383 
385 {
386  Printf("TGPicture: %s,%sref cnt = %u %lx", GetName(),
387  fScaled ? " scaled, " : " ", References(), fPic);
388 }
389 
390 
391 ////////////////////////////////////////////////////////////////////////////////
392 /// Create a "selected" looking picture based on the original TGPicture.
393 
395  TGPicture("")
396 {
397  GCValues_t gcv;
398  UInt_t w, h;
399 
400  fClient = client;
401  Window_t root = fClient->GetDefaultRoot()->GetId();
402 
403  w = p->GetWidth();
404  h = p->GetHeight();
405 
406  fPic = gVirtualX->CreatePixmap(root, w, h);
407  fMask = p->GetMask();
408 
409  fAttributes.fWidth = w;
411 
412  gVirtualX->CopyArea(p->GetPicture(), fPic, GetSelectedGC()(), 0, 0, w, h, 0, 0);
413 
415  gcv.fClipMask = p->GetMask();
416  gcv.fClipXOrigin = 0;
417  gcv.fClipYOrigin = 0;
419 
420  gVirtualX->FillRectangle(fPic, GetSelectedGC()(), 0, 0, w, h);
421 
423 }
424 
425 ////////////////////////////////////////////////////////////////////////////////
426 /// Delete selected picture.
427 
429 {
430  // fMask was borrowed so should not be deleted by ~TGPicture.
431  fMask = kNone;
432 }
433 
434 ////////////////////////////////////////////////////////////////////////////////
435 /// Return selection graphics context in use.
436 
438 {
439  if (!fgSelectedGC) {
440  fgSelectedGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
441  fgSelectedGC->SetForeground(gClient->GetResourcePool()->GetSelectedBgndColor());
442  fgSelectedGC->SetBackground(gClient->GetResourcePool()->GetBlackColor());
444  fgSelectedGC->SetStipple(gClient->GetResourcePool()->GetCheckeredBitmap());
445  }
446  return *fgSelectedGC;
447 }
Int_t fClipXOrigin
Definition: GuiTypes.h:244
void Print(Option_t *option="") const
List all pictures in the pool.
Definition: TGPicture.cxx:305
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:862
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:373
const char * GetName() const
Returns name of object.
Definition: TGPicture.h:62
TGPicturePool(const TGPicturePool &)
copy constructor
Definition: TGPicture.cxx:54
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition: TROOT.h:393
const char Option_t
Definition: RtypesCore.h:64
void SetForeground(Pixel_t v)
Set foreground color.
Definition: TGGC.cxx:276
ULong_t * fPixels
Definition: GuiTypes.h:329
Handle_t GContext_t
Definition: GuiTypes.h:37
Bool_t IsScaled() const
Definition: TGPicture.h:67
Basic string class.
Definition: TString.h:131
#define gClient
Definition: TGClient.h:166
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition: TSystem.cxx:1541
PictureAttributes_t fAttributes
Definition: TGPicture.h:45
Int_t fClipYOrigin
Definition: GuiTypes.h:245
An abstract interface to image processing library.
Definition: TImage.h:29
const TGClient * fClient
Definition: TGPicture.h:103
Pixmap_t fClipMask
Definition: GuiTypes.h:246
UInt_t GetHeight() const
Definition: TGPicture.h:64
virtual ~TGSelectedPicture()
Delete selected picture.
Definition: TGPicture.cxx:428
Handle_t GetId() const
Definition: TGObject.h:47
virtual Pixmap_t GetPixmap()
Definition: TImage.h:235
TGSelectedPicture(const TGSelectedPicture &gp)
Definition: TGPicture.h:86
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1058
virtual void Scale(UInt_t, UInt_t)
Definition: TImage.h:141
void Draw(Option_t *="")
Default Draw method for all objects.
Definition: TGPicture.h:57
void SetBackground(Pixel_t v)
Set background color.
Definition: TGGC.cxx:287
const Mask_t kGCClipMask
Definition: GuiTypes.h:304
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:35
Double_t x[n]
Definition: legend1.C:17
static TGGC * fgSelectedGC
Definition: TGPicture.h:83
const Mask_t kPASize
Definition: GuiTypes.h:338
void FreePicture(const TGPicture *pic)
Remove picture from cache if nobody is using it anymore.
Definition: TGPicture.cxx:275
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:268
void SetStipple(Pixmap_t v)
Set 1 plane pixmap for stippling.
Definition: TGGC.cxx:376
TString fPath
Definition: TGPicture.h:104
const Mask_t kPAColormap
Definition: GuiTypes.h:336
Pixmap_t GetPicture() const
Definition: TGPicture.h:65
void SetRefCount(UInt_t r)
Definition: TRefCnt.h:39
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
XPoint xy[kMAXMK]
Definition: TGX11.cxx:122
const Mask_t kGCClipXOrigin
Definition: GuiTypes.h:302
void SetFillStyle(Int_t v)
Set fill style (kFillSolid, kFillTiled, kFillStippled, kFillOpaeueStippled).
Definition: TGGC.cxx:343
virtual ~TGPicture()
Delete picture object.
Definition: TGPicture.cxx:359
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
unsigned int UInt_t
Definition: RtypesCore.h:44
const Handle_t kNone
Definition: GuiTypes.h:87
Ssiz_t Length() const
Definition: TString.h:405
const TGPicture * GetPicture(const char *name)
Get a picture from the picture pool.
Definition: TGPicture.cxx:80
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1106
const Mask_t kGCClipYOrigin
Definition: GuiTypes.h:303
Bool_t fScaled
Definition: TGPicture.h:42
UInt_t References() const
Definition: TRefCnt.h:38
#define gVirtualX
Definition: TVirtualX.h:338
#define h(i)
Definition: RSha256.hxx:106
static TGGC & GetSelectedGC()
Return selection graphics context in use.
Definition: TGPicture.cxx:437
const Bool_t kFALSE
Definition: RtypesCore.h:90
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
THashTable * fPicList
Definition: TGPicture.h:105
void Print(Option_t *option, Int_t recurse) const
Print the collection header and its elements.
Definition: THashTable.cxx:325
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:238
#define ClassImp(name)
Definition: Rtypes.h:361
virtual ~TGPicturePool()
Delete picture cache.
Definition: TGPicture.cxx:291
virtual Pixmap_t GetMask()
Definition: TImage.h:236
UInt_t RemoveReference()
Definition: TRefCnt.h:41
void Printf(const char *fmt,...)
Pixmap_t fMask
Definition: TGPicture.h:44
void Print(Option_t *option="") const
Print picture info.
Definition: TGPicture.cxx:384
TGPicture(const char *name, Bool_t scaled=kFALSE)
Definition: TGPicture.h:47
Double_t y[n]
Definition: legend1.C:17
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:92
UInt_t GetWidth() const
Definition: TGPicture.h:63
TString fName
Definition: TGPicture.h:41
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:154
void Delete(Option_t *option="")
Remove all objects from the table AND delete all heap based objects.
Definition: THashTable.cxx:220
Handle_t Window_t
Definition: GuiTypes.h:28
Mask_t fMask
Definition: GuiTypes.h:250
void AddReference()
Definition: TRefCnt.h:40
Colormap_t fColormap
Definition: GuiTypes.h:323
Handle_t Pixmap_t
Definition: GuiTypes.h:29
const TGWindow * GetDefaultRoot() const
Returns the root (i.e.
Definition: TGClient.cxx:234
Pixmap_t GetMask() const
Definition: TGPicture.h:66
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition: TImage.cxx:119
TGPicturePool & operator=(const TGPicturePool &)
assignment operator
Definition: TGPicture.cxx:65
const Mask_t kPACloseness
Definition: GuiTypes.h:341
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1269
Pixmap_t fPic
Definition: TGPicture.h:43
void SetClipMask(Pixmap_t v)
Bitmap for clipping.
Definition: TGGC.cxx:464
const Bool_t kTRUE
Definition: RtypesCore.h:89
Definition: TGGC.h:31
char name[80]
Definition: TGX11.cxx:109
const TGClient * fClient
Definition: TGPicture.h:81
TObject * Remove(TObject *obj)
Remove object from the hashtable.
Definition: THashTable.cxx:414
const char * Data() const
Definition: TString.h:364
ULong_t Handle_t
Definition: GuiTypes.h:25