Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/** \class TGPicture
25 \ingroup guiwidgets
26
27
28The TGPicture class implements pictures and icons used in the
29different GUI elements and widgets. The TGPicturePool class
30implements a TGPicture cache. TGPictures are created, managed and
31destroyed by the TGPicturePool.
32
33*/
34
35
36#include "TGPicture.h"
37#include "TGResourcePool.h"
38#include "THashTable.h"
39#include "TSystem.h"
40#include "TGWindow.h"
41#include "TVirtualX.h"
42#include "TImage.h"
43#include "TROOT.h"
44#include <cstdlib>
45
47
51
52
53////////////////////////////////////////////////////////////////////////////////
54///copy constructor
55
57 TObject(pp),
58 fClient(pp.fClient),
59 fPath(pp.fPath),
60 fPicList(pp.fPicList)
61{
62}
63
64////////////////////////////////////////////////////////////////////////////////
65///assignment operator
66
68{
69 if(this!=&pp) {
72 fPath=pp.fPath;
74 }
75 return *this;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Get a picture from the picture pool. Picture must be freed using
80/// TGPicturePool::FreePicture(). If picture is not found 0 is returned.
81/// \param name Name of the file containing the picture
82
84{
85 if (!fPicList)
86 fPicList = new THashTable(50);
87
88 TString pname = name;
89 pname.Strip();
90 TString ext = strrchr(pname, '.');
91 ext.ToLower();
92
93 if (ext.Length()) { // ".xpm", ".gif" etc
94 pname = gSystem->UnixPathName(pname);
95 gSystem->ExpandPathName(pname);
96 }
97
98 TGPicture *pic = (TGPicture *)fPicList->FindObject(pname);
99 if (pic && !pic->IsScaled()) {
100 if (pic->fPic == kNone)
101 return 0;
102 pic->AddReference();
103 return pic;
104 }
105
106 char *picnam = gSystem->Which(fPath, pname, kReadPermission);
107 if (!picnam) {
108 pic = new TGPicture(pname);
110 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
112 fPicList->Add(pic);
113 return 0;
114 }
115
116 TImage *img = TImage::Open(picnam);
117 if (!img) {
118 pic = new TGPicture(pname);
120 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
122 fPicList->Add(pic);
123 delete [] picnam;
124 return 0;
125 }
126
127 pic = new TGPicture(pname, img->GetPixmap(), img->GetMask());
128 delete [] picnam;
129 delete img;
130 fPicList->Add(pic);
131 return pic;
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Like TGPicturePool::GetPicture() but, instead of returning null when the
136/// picture is not found, it returns a valid empty picture.
137
139{
140 static const TGPicture fEmptyPic { "Empty" };
141 const TGPicture *pic = GetPicture(name);
142 if (!pic)
143 pic = &fEmptyPic;
144 return pic;
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Get picture with specified size from pool (picture will be scaled if
149/// necessary). Picture must be freed using TGPicturePool::FreePicture(). If
150/// picture is not found 0 is returned.
151
153 UInt_t new_width, UInt_t new_height)
154{
155 if (!fPicList)
156 fPicList = new THashTable(50);
157
158 TString pname = name;
159 pname.Strip();
160 TString ext = strrchr(pname, '.');
161 ext.ToLower();
162
163 if (ext.Length()) { // ".xpm", ".gif" etc
164 pname = gSystem->UnixPathName(pname);
165 gSystem->ExpandPathName(pname);
166 }
167
168 const char *hname = TGPicture::HashName(pname, new_width, new_height);
169 TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
170 if (pic && pic->GetWidth() == new_width && pic->GetHeight() == new_height) {
171 if (pic->fPic == kNone)
172 return 0;
173 pic->AddReference();
174 return pic;
175 }
176
177 char *picnam = gSystem->Which(fPath, pname, kReadPermission);
178 if (!picnam) {
179 pic = new TGPicture(hname, kTRUE);
181 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
183 pic->fAttributes.fWidth = new_width;
184 pic->fAttributes.fHeight = new_height;
185 fPicList->Add(pic);
186 return 0;
187 }
188
189 TImage *img = TImage::Open(picnam);
190 if (!img) {
191 pic = new TGPicture(hname, kTRUE);
193 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
195 pic->fAttributes.fWidth = new_width;
196 pic->fAttributes.fHeight = new_height;
197 fPicList->Add(pic);
198 delete [] picnam;
199 return 0;
200 }
201
202 img->Scale(new_width, new_height);
203
204 pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
205 delete [] picnam;
206 delete img;
207 fPicList->Add(pic);
208 return pic;
209}
210
211////////////////////////////////////////////////////////////////////////////////
212/// Get picture with specified pixmap and mask from pool.
213/// Picture must be freed using TGPicturePool::FreePicture().
214/// If picture is not found 0 is returned.
215
218{
219 if (!fPicList)
220 fPicList = new THashTable(50);
221
222 Int_t xy;
223 UInt_t w, h;
224
225 gVirtualX->GetWindowSize(pxmap, xy, xy, w, h);
226
227 const char *hname = TGPicture::HashName(name, w, h);
228 TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
229
230 if (pic) {
231 pic->AddReference();
232 return pic;
233 }
234
235 pic = new TGPicture(hname, pxmap, mask);
236 fPicList->Add(pic);
237
238 return pic;
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Create picture from XPM data.
243/// Picture must be freed using TGPicturePool::FreePicture().
244/// If picture creation failed 0 is returned.
245
246const TGPicture *TGPicturePool::GetPicture(const char *name, char **xpm)
247{
248 UInt_t w, h;
249
250 if (!xpm || !*xpm) {
251 return 0;
252 }
253
254 if (!fPicList) {
255 fPicList = new THashTable(50);
256 }
257 char *ptr = xpm[0];
258 while (isspace((int)*ptr)) ++ptr;
259 w = atoi(ptr);
260
261 while (isspace((int)*ptr)) ++ptr;
262 h = atoi(ptr);
263
264 const char *hname = TGPicture::HashName(name, w, h);
265 TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
266 if (pic) {
267 pic->AddReference();
268 return pic;
269 }
270
271 TImage *img = TImage::Open(xpm);
272 if (!img) {
273 pic = new TGPicture(hname, kTRUE);
275 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
277 pic->fAttributes.fWidth = w;
278 pic->fAttributes.fHeight = h;
279 fPicList->Add(pic);
280 return 0;
281 }
282
283 pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
284 delete img;
285 return pic;
286}
287
288////////////////////////////////////////////////////////////////////////////////
289/// Remove picture from cache if nobody is using it anymore.
290
292{
293 if (!fPicList) return;
294
295 TGPicture *pic = (TGPicture *)fPicList->FindObject(fpic);
296 if (pic) {
297 if (pic->RemoveReference() == 0) {
298 fPicList->Remove(pic);
299 delete pic;
300 }
301 }
302}
303
304////////////////////////////////////////////////////////////////////////////////
305/// Delete picture cache.
306
308{
309 if (fPicList) {
310 fPicList->Delete();
311 delete fPicList;
312 }
313
314 // Required since we overload TObject::Hash.
316}
317
318////////////////////////////////////////////////////////////////////////////////
319/// List all pictures in the pool.
320
322{
323 if (fPicList)
324 fPicList->Print();
325 else
326 Info("Print", "no pictures in picture pool");
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// ctor. Important: both pixmaps pxmap and mask must be unique (not shared)
331
333{
334 fName = name;
335 fScaled = kFALSE;
336 fPic = pxmap;
337 fMask = mask;
338 Int_t xy;
339
340 fAttributes.fColormap = gClient->GetDefaultColormap();
341 fAttributes.fCloseness = 40000; // Allow for "similar" colors
348
350 SetRefCount(1);
351}
352
353////////////////////////////////////////////////////////////////////////////////
354/// Draw a picture.
355
357{
358 GCValues_t gcv;
359
361 gcv.fClipMask = fMask;
362 gcv.fClipXOrigin = x;
363 gcv.fClipYOrigin = y;
364 gVirtualX->ChangeGC(gc, &gcv);
365 gVirtualX->CopyArea(fPic, id, gc, 0, 0, fAttributes.fWidth, fAttributes.fHeight,
366 x, y);
367 gcv.fMask = kGCClipMask;
368 gcv.fClipMask = kNone;
369 gVirtualX->ChangeGC(gc, &gcv);
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Delete picture object.
374
376{
377 if (fPic != kNone)
378 gVirtualX->DeletePixmap(fPic);
379 if (fMask != kNone)
380 gVirtualX->DeletePixmap(fMask);
382 delete [] fAttributes.fPixels;
383}
384
385////////////////////////////////////////////////////////////////////////////////
386/// Static function returning a unique name used to look up a picture.
387/// The unique name has the form "name__widthxheight".
388
389const char *TGPicture::HashName(const char *name, Int_t width, Int_t height)
390{
391 static TString hashName;
392
393 hashName.Form("%s__%dx%d", name, width, height);
394 return hashName.Data();
395}
396
397////////////////////////////////////////////////////////////////////////////////
398/// Print picture info.
399
401{
402 Printf("TGPicture: %s,%sref cnt = %u %lx", GetName(),
403 fScaled ? " scaled, " : " ", References(), fPic);
404}
405
406
407////////////////////////////////////////////////////////////////////////////////
408/// Create a "selected" looking picture based on the original TGPicture.
409
411 TGPicture("")
412{
413 GCValues_t gcv;
414 UInt_t w, h;
415
416 fClient = client;
418
419 w = p->GetWidth();
420 h = p->GetHeight();
421
422 fPic = gVirtualX->CreatePixmap(root, w, h);
423 fMask = p->GetMask();
424
427
428 gVirtualX->CopyArea(p->GetPicture(), fPic, GetSelectedGC()(), 0, 0, w, h, 0, 0);
429
431 gcv.fClipMask = p->GetMask();
432 gcv.fClipXOrigin = 0;
433 gcv.fClipYOrigin = 0;
435
436 gVirtualX->FillRectangle(fPic, GetSelectedGC()(), 0, 0, w, h);
437
439}
440
441////////////////////////////////////////////////////////////////////////////////
442/// Delete selected picture.
443
445{
446 // fMask was borrowed so should not be deleted by ~TGPicture.
447 fMask = kNone;
448}
449
450////////////////////////////////////////////////////////////////////////////////
451/// Return selection graphics context in use.
452
454{
455 if (!fgSelectedGC) {
456 fgSelectedGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
457 fgSelectedGC->SetForeground(gClient->GetResourcePool()->GetSelectedBgndColor());
458 fgSelectedGC->SetBackground(gClient->GetResourcePool()->GetBlackColor());
460 fgSelectedGC->SetStipple(gClient->GetResourcePool()->GetCheckeredBitmap());
461 }
462 return *fgSelectedGC;
463}
Handle_t Pixmap_t
Pixmap handle.
Definition GuiTypes.h:30
const Mask_t kGCClipXOrigin
Definition GuiTypes.h:303
const Mask_t kPACloseness
Definition GuiTypes.h:342
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:38
const Handle_t kNone
Definition GuiTypes.h:88
@ kFillStippled
Definition GuiTypes.h:51
const Mask_t kPAColormap
Definition GuiTypes.h:337
const Mask_t kGCClipYOrigin
Definition GuiTypes.h:304
const Mask_t kPASize
width and height
Definition GuiTypes.h:339
const Mask_t kGCClipMask
Definition GuiTypes.h:305
ULongptr_t Handle_t
Generic resource handle.
Definition GuiTypes.h:26
#define h(i)
Definition RSha256.hxx:106
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
#define gClient
Definition TGClient.h:157
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint xy
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
char name[80]
Definition TGX11.cxx:110
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2503
@ kReadPermission
Definition TSystem.h:45
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
#define gVirtualX
Definition TVirtualX.h:337
Window client.
Definition TGClient.h:37
const TGWindow * GetDefaultRoot() const
Returns the root (i.e.
Definition TGClient.cxx:234
Colormap_t GetDefaultColormap() const
Definition TGClient.h:146
Encapsulate a graphics context used in the low level graphics.
Definition TGGC.h:22
void SetFillStyle(Int_t v)
Set fill style (kFillSolid, kFillTiled, kFillStippled, kFillOpaeueStippled).
Definition TGGC.cxx:345
void SetForeground(Pixel_t v)
Set foreground color.
Definition TGGC.cxx:278
void SetClipMask(Pixmap_t v)
Bitmap for clipping.
Definition TGGC.cxx:466
void SetAttributes(GCValues_t *values)
Set attributes as specified in the values structure.
Definition TGGC.cxx:235
void SetBackground(Pixel_t v)
Set background color.
Definition TGGC.cxx:289
void SetStipple(Pixmap_t v)
Set 1 plane pixmap for stippling.
Definition TGGC.cxx:378
Handle_t GetId() const
Definition TGObject.h:41
TString fPath
icon search path
Definition TGPicture.h:92
THashTable * fPicList
hash table containing the icons
Definition TGPicture.h:93
TGPicturePool(const TGPicturePool &)
copy constructor
Definition TGPicture.cxx:56
void Print(Option_t *option="") const override
List all pictures in the pool.
~TGPicturePool() override
Delete picture cache.
void FreePicture(const TGPicture *pic)
Remove picture from cache if nobody is using it anymore.
const TGClient * fClient
client for which we keep icon pool
Definition TGPicture.h:91
const TGPicture * GetPictureOrEmpty(const char *name)
Like TGPicturePool::GetPicture() but, instead of returning null when the picture is not found,...
const TGPicture * GetPicture(const char *name)
Get a picture from the picture pool.
Definition TGPicture.cxx:83
TGPicturePool & operator=(const TGPicturePool &)
assignment operator
Definition TGPicture.cxx:67
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
Pixmap_t fPic
picture pixmap
Definition TGPicture.h:32
PictureAttributes_t fAttributes
picture attributes
Definition TGPicture.h:34
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.
void Print(Option_t *option="") const override
Print picture info.
Pixmap_t fMask
picture mask pixmap
Definition TGPicture.h:33
Bool_t IsScaled() const
Definition TGPicture.h:56
void Draw(Option_t *="") override
Default Draw method for all objects.
Definition TGPicture.h:46
TGPicture(const char *name, Bool_t scaled=kFALSE)
Definition TGPicture.h:36
UInt_t GetHeight() const
Definition TGPicture.h:53
~TGPicture() override
Delete picture object.
const char * GetName() const override
Returns name of object.
Definition TGPicture.h:51
Bool_t fScaled
kTRUE if picture is scaled
Definition TGPicture.h:31
TString fName
name of picture
Definition TGPicture.h:30
UInt_t GetWidth() const
Definition TGPicture.h:52
static TGGC & GetSelectedGC()
Return selection graphics context in use.
~TGSelectedPicture() override
Delete selected picture.
static TGGC * fgSelectedGC
Definition TGPicture.h:72
TGSelectedPicture(const TGSelectedPicture &gp)
Definition TGPicture.h:75
const TGClient * fClient
Definition TGPicture.h:70
THashTable implements a hash table to store TObject's.
Definition THashTable.h:35
void Add(TObject *obj) override
Add object to the hash table.
TObject * Remove(TObject *obj) override
Remove object from the hashtable.
TObject * FindObject(const char *name) const override
Find object using its name.
void Print(Option_t *option, Int_t recurse) const override
Print the collection header and its elements.
void Delete(Option_t *option="") override
Remove all objects from the table AND delete all heap based objects.
An abstract interface to image processing library.
Definition TImage.h:29
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition TImage.cxx:118
virtual void Scale(UInt_t, UInt_t)
Definition TImage.h:141
virtual Pixmap_t GetPixmap()
Definition TImage.h:235
virtual Pixmap_t GetMask()
Definition TImage.h:236
Mother of all ROOT objects.
Definition TObject.h:41
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition TObject.h:296
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:979
void AddReference()
Definition TRefCnt.h:40
void SetRefCount(UInt_t r)
Definition TRefCnt.h:39
UInt_t RemoveReference()
Definition TRefCnt.h:41
UInt_t References() const
Definition TRefCnt.h:38
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition TString.cxx:1163
const char * Data() const
Definition TString.h:376
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1274
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1063
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition TSystem.cxx:1548
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition TROOT.h:395
Graphics context structure.
Definition GuiTypes.h:224
Pixmap_t fClipMask
bitmap clipping; other calls for rects
Definition GuiTypes.h:247
Int_t fClipYOrigin
Definition GuiTypes.h:246
Int_t fClipXOrigin
origin for clipping
Definition GuiTypes.h:245
Mask_t fMask
bit mask specifying which fields are valid
Definition GuiTypes.h:251
UInt_t fHeight
height of picture
Definition GuiTypes.h:327
Mask_t fMask
mask specifying which attributes are defined
Definition GuiTypes.h:333
ULong_t * fPixels
list of used color pixels (if set use delete[])
Definition GuiTypes.h:330
Colormap_t fColormap
colormap to use
Definition GuiTypes.h:324
UInt_t fCloseness
allowable RGB deviation
Definition GuiTypes.h:332
Int_t fDepth
depth of window
Definition GuiTypes.h:325
UInt_t fNpixels
number of used color pixels
Definition GuiTypes.h:331
UInt_t fYHotspot
picture y hotspot coordinate
Definition GuiTypes.h:329
UInt_t fXHotspot
picture x hotspot coordinate
Definition GuiTypes.h:328
UInt_t fWidth
width of picture
Definition GuiTypes.h:326