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 "TASUtils.h"
44#include "TROOT.h"
45#include <cstdlib>
46
48
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) {
70 fPath=pp.fPath;
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/// \param name Name of the file containing the picture
80
82{
83 if (!fPicList)
84 fPicList = new THashTable(50);
85
86 TString pname = name;
87 pname.Strip();
88 TString ext = strrchr(pname, '.');
89 ext.ToLower();
90
91 if (ext.Length()) { // ".xpm", ".gif" etc
92 pname = gSystem->UnixPathName(pname);
93 gSystem->ExpandPathName(pname);
94 }
95
96 TGPicture *pic = (TGPicture *)fPicList->FindObject(pname);
97 if (pic && !pic->IsScaled()) {
98 // in batch mode allow to return dummy image
99 if ((pic->fPic == kNone) && !gROOT->IsBatch())
100 return nullptr;
101 pic->AddReference();
102 return pic;
103 }
104
105 char *picnam = gSystem->Which(fPath, pname, kReadPermission);
106
107 TImage *img = picnam ? TImage::Open(picnam) : nullptr;
108
109 delete [] picnam;
110
111 if (!img) {
112 pic = new TGPicture(pname);
114 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
116 fPicList->Add(pic);
117 return nullptr;
118 }
119
120 pic = new TGPicture(pname, TASUtils::GetPixmap(img), TASUtils::GetMask(img));
121 delete img;
122 fPicList->Add(pic);
123 return pic;
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Like TGPicturePool::GetPicture() but, instead of returning null when the
128/// picture is not found, it returns a valid empty picture.
129
131{
132 static const TGPicture fEmptyPic { "Empty" };
133 const TGPicture *pic = GetPicture(name);
134 if (!pic)
135 pic = &fEmptyPic;
136 return pic;
137}
138
139////////////////////////////////////////////////////////////////////////////////
140/// Get picture with specified size from pool (picture will be scaled if
141/// necessary). Picture must be freed using TGPicturePool::FreePicture(). If
142/// picture is not found 0 is returned.
143
145 UInt_t new_width, UInt_t new_height)
146{
147 if (!fPicList)
148 fPicList = new THashTable(50);
149
150 TString pname = name;
151 pname.Strip();
152 TString ext = strrchr(pname, '.');
153 ext.ToLower();
154
155 if (ext.Length()) { // ".xpm", ".gif" etc
156 pname = gSystem->UnixPathName(pname);
157 gSystem->ExpandPathName(pname);
158 }
159
160 const char *hname = TGPicture::HashName(pname, new_width, new_height);
161 TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
162 if (pic && pic->GetWidth() == new_width && pic->GetHeight() == new_height) {
163 if (pic->fPic == kNone)
164 return 0;
165 pic->AddReference();
166 return pic;
167 }
168
169 char *picnam = gSystem->Which(fPath, pname, kReadPermission);
170 if (!picnam) {
171 pic = new TGPicture(hname, kTRUE);
173 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
175 pic->fAttributes.fWidth = new_width;
176 pic->fAttributes.fHeight = new_height;
177 fPicList->Add(pic);
178 return 0;
179 }
180
181 TImage *img = TImage::Open(picnam);
182 if (!img) {
183 pic = new TGPicture(hname, kTRUE);
185 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
187 pic->fAttributes.fWidth = new_width;
188 pic->fAttributes.fHeight = new_height;
189 fPicList->Add(pic);
190 delete [] picnam;
191 return 0;
192 }
193
194 img->Scale(new_width, new_height);
195
196 pic = new TGPicture(hname, TASUtils::GetPixmap(img), TASUtils::GetMask(img));
197 delete [] picnam;
198 delete img;
199 fPicList->Add(pic);
200 return pic;
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// Get picture with specified pixmap and mask from pool.
205/// Picture must be freed using TGPicturePool::FreePicture().
206/// If picture is not found 0 is returned.
207
210{
211 if (!fPicList)
212 fPicList = new THashTable(50);
213
214 Int_t xy;
215 UInt_t w, h;
216
217 gVirtualX->GetWindowSize(pxmap, xy, xy, w, h);
218
219 const char *hname = TGPicture::HashName(name, w, h);
220 TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
221
222 if (pic) {
223 pic->AddReference();
224 return pic;
225 }
226
227 pic = new TGPicture(hname, pxmap, mask);
228 fPicList->Add(pic);
229
230 return pic;
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// Create picture from XPM data.
235/// Picture must be freed using TGPicturePool::FreePicture().
236/// If picture creation failed 0 is returned.
237
238const TGPicture *TGPicturePool::GetPicture(const char *name, char **xpm)
239{
240 UInt_t w, h;
241
242 if (!xpm || !*xpm) {
243 return 0;
244 }
245
246 if (!fPicList) {
247 fPicList = new THashTable(50);
248 }
249 char *ptr = xpm[0];
250 while (isspace((int)*ptr)) ++ptr;
251 w = atoi(ptr);
252
253 while (isspace((int)*ptr)) ++ptr;
254 h = atoi(ptr);
255
256 const char *hname = TGPicture::HashName(name, w, h);
257 TGPicture *pic = (TGPicture *)fPicList->FindObject(hname);
258 if (pic) {
259 pic->AddReference();
260 return pic;
261 }
262
263 TImage *img = TImage::Open(xpm);
264 if (!img) {
265 pic = new TGPicture(hname, kTRUE);
267 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
269 pic->fAttributes.fWidth = w;
270 pic->fAttributes.fHeight = h;
271 fPicList->Add(pic);
272 return 0;
273 }
274
275 pic = new TGPicture(hname, TASUtils::GetPixmap(img), TASUtils::GetMask(img));
276 delete img;
277 return pic;
278}
279
280////////////////////////////////////////////////////////////////////////////////
281/// Remove picture from cache if nobody is using it anymore.
282
284{
285 if (!fPicList) return;
286
287 TGPicture *pic = (TGPicture *)fPicList->FindObject(fpic);
288 if (pic) {
289 if (pic->RemoveReference() == 0) {
290 fPicList->Remove(pic);
291 delete pic;
292 }
293 }
294}
295
296////////////////////////////////////////////////////////////////////////////////
297/// Delete picture cache.
298
300{
301 if (fPicList) {
302 fPicList->Delete();
303 delete fPicList;
304 }
305
306 // Required since we overload TObject::Hash.
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// List all pictures in the pool.
312
314{
315 if (fPicList)
316 fPicList->Print();
317 else
318 Info("Print", "no pictures in picture pool");
319}
320
321////////////////////////////////////////////////////////////////////////////////
322/// ctor. Important: both pixmaps pxmap and mask must be unique (not shared)
323
325{
326 fName = name;
327 fScaled = kFALSE;
328 fPic = pxmap;
329 fMask = mask;
330 Int_t xy;
331
332 fAttributes.fColormap = gClient->GetDefaultColormap();
333 fAttributes.fCloseness = 40000; // Allow for "similar" colors
340
342 SetRefCount(1);
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Draw a picture.
347
349{
350 GCValues_t gcv;
351
353 gcv.fClipMask = fMask;
354 gcv.fClipXOrigin = x;
355 gcv.fClipYOrigin = y;
356 gVirtualX->ChangeGC(gc, &gcv);
357 gVirtualX->CopyArea(fPic, id, gc, 0, 0, fAttributes.fWidth, fAttributes.fHeight,
358 x, y);
359 gcv.fMask = kGCClipMask;
360 gcv.fClipMask = kNone;
361 gVirtualX->ChangeGC(gc, &gcv);
362}
363
364////////////////////////////////////////////////////////////////////////////////
365/// Delete picture object.
366
368{
369 if (fPic != kNone)
370 gVirtualX->DeletePixmap(fPic);
371 if (fMask != kNone)
372 gVirtualX->DeletePixmap(fMask);
374 delete [] fAttributes.fPixels;
375}
376
377////////////////////////////////////////////////////////////////////////////////
378/// Static function returning a unique name used to look up a picture.
379/// The unique name has the form "name__widthxheight".
380
381const char *TGPicture::HashName(const char *name, Int_t width, Int_t height)
382{
383 static TString hashName;
384
385 hashName.Form("%s__%dx%d", name, width, height);
386 return hashName.Data();
387}
388
389////////////////////////////////////////////////////////////////////////////////
390/// Print picture info.
391
393{
394 Printf("TGPicture: %s,%sref cnt = %u %lx", GetName(),
395 fScaled ? " scaled, " : " ", References(), fPic);
396}
397
398
399////////////////////////////////////////////////////////////////////////////////
400/// Create a "selected" looking picture based on the original TGPicture.
401
403 TGPicture("")
404{
405 GCValues_t gcv;
406 UInt_t w, h;
407
408 fClient = client;
410
411 w = p->GetWidth();
412 h = p->GetHeight();
413
414 fPic = gVirtualX->CreatePixmap(root, w, h);
415 fMask = p->GetMask();
416
419
420 gVirtualX->CopyArea(p->GetPicture(), fPic, GetSelectedGC()(), 0, 0, w, h, 0, 0);
421
423 gcv.fClipMask = p->GetMask();
424 gcv.fClipXOrigin = 0;
425 gcv.fClipYOrigin = 0;
427
428 gVirtualX->FillRectangle(fPic, GetSelectedGC()(), 0, 0, w, h);
429
431}
432
433////////////////////////////////////////////////////////////////////////////////
434/// Delete selected picture.
435
437{
438 // fMask was borrowed so should not be deleted by ~TGPicture.
439 fMask = kNone;
440}
441
442////////////////////////////////////////////////////////////////////////////////
443/// Return selection graphics context in use.
444
446{
447 if (!fgSelectedGC) {
448 fgSelectedGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
449 fgSelectedGC->SetForeground(gClient->GetResourcePool()->GetSelectedBgndColor());
450 fgSelectedGC->SetBackground(gClient->GetResourcePool()->GetBlackColor());
452 fgSelectedGC->SetStipple(gClient->GetResourcePool()->GetCheckeredBitmap());
453 }
454 return *fgSelectedGC;
455}
Handle_t Pixmap_t
Pixmap handle.
Definition GuiTypes.h:31
const Mask_t kGCClipXOrigin
Definition GuiTypes.h:304
const Mask_t kPACloseness
Definition GuiTypes.h:343
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:39
const Handle_t kNone
Definition GuiTypes.h:89
@ kFillStippled
Definition GuiTypes.h:52
const Mask_t kPAColormap
Definition GuiTypes.h:338
const Mask_t kGCClipYOrigin
Definition GuiTypes.h:305
const Mask_t kPASize
width and height
Definition GuiTypes.h:340
const Mask_t kGCClipMask
Definition GuiTypes.h:306
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:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
#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:142
#define gROOT
Definition TROOT.h:417
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2585
@ kReadPermission
Definition TSystem.h:55
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
#define gVirtualX
Definition TVirtualX.h:379
static Pixmap_t GetPixmap(TImage *timg)
Returns image pixmap.
Definition TASUtils.cxx:135
static Pixmap_t GetMask(TImage *timg)
Returns image mask pixmap (alpha channel).
Definition TASUtils.cxx:181
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:337
void SetForeground(Pixel_t v)
Set foreground color.
Definition TGGC.cxx:270
void SetClipMask(Pixmap_t v)
Bitmap for clipping.
Definition TGGC.cxx:458
void SetAttributes(GCValues_t *values)
Set attributes as specified in the values structure.
Definition TGGC.cxx:227
void SetBackground(Pixel_t v)
Set background color.
Definition TGGC.cxx:281
void SetStipple(Pixmap_t v)
Set 1 plane pixmap for stippling.
Definition TGGC.cxx:370
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:54
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:81
TGPicturePool & operator=(const TGPicturePool &)
assignment operator
Definition TGPicture.cxx:65
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:30
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition TImage.cxx:123
Mother of all ROOT objects.
Definition TObject.h:42
TObject & operator=(const TObject &rhs) noexcept
TObject assignment operator.
Definition TObject.h:305
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1070
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:137
Ssiz_t Length() const
Definition TString.h:426
void ToLower()
Change string to lower-case.
Definition TString.cxx:1190
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition TString.cxx:1171
const char * Data() const
Definition TString.h:385
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2438
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1296
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1078
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition TSystem.cxx:1570
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:406
Graphics context structure.
Definition GuiTypes.h:225
Pixmap_t fClipMask
bitmap clipping; other calls for rects
Definition GuiTypes.h:248
Int_t fClipYOrigin
Definition GuiTypes.h:247
Int_t fClipXOrigin
origin for clipping
Definition GuiTypes.h:246
Mask_t fMask
bit mask specifying which fields are valid
Definition GuiTypes.h:252
UInt_t fHeight
height of picture
Definition GuiTypes.h:328
Mask_t fMask
mask specifying which attributes are defined
Definition GuiTypes.h:334
ULong_t * fPixels
list of used color pixels (if set use delete[])
Definition GuiTypes.h:331
Colormap_t fColormap
colormap to use
Definition GuiTypes.h:325
UInt_t fCloseness
allowable RGB deviation
Definition GuiTypes.h:333
Int_t fDepth
depth of window
Definition GuiTypes.h:326
UInt_t fNpixels
number of used color pixels
Definition GuiTypes.h:332
UInt_t fYHotspot
picture y hotspot coordinate
Definition GuiTypes.h:330
UInt_t fXHotspot
picture x hotspot coordinate
Definition GuiTypes.h:329
UInt_t fWidth
width of picture
Definition GuiTypes.h:327