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
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) {
69 fPath=pp.fPath;
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/// \param name Name of the file containing the picture
79
81{
82 if (!fPicList)
83 fPicList = new THashTable(50);
84
86 pname.Strip();
87 TString ext = strrchr(pname, '.');
88 ext.ToLower();
89
90 if (ext.Length()) { // ".xpm", ".gif" etc
93 }
94
96 if (pic && !pic->IsScaled()) {
97 if (pic->fPic == kNone)
98 return 0;
99 pic->AddReference();
100 return pic;
101 }
102
104 if (!picnam) {
105 pic = new TGPicture(pname);
106 pic->fAttributes.fColormap = fClient->GetDefaultColormap();
107 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
108 pic->fAttributes.fMask = kPASize | kPAColormap | kPACloseness;
109 fPicList->Add(pic);
110 return 0;
111 }
112
114 if (!img) {
115 pic = new TGPicture(pname);
116 pic->fAttributes.fColormap = fClient->GetDefaultColormap();
117 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
118 pic->fAttributes.fMask = kPASize | kPAColormap | kPACloseness;
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/// Like TGPicturePool::GetPicture() but, instead of returning null when the
133/// picture is not found, it returns a valid empty picture.
134
136{
137 static const TGPicture fEmptyPic { "Empty" };
138 const TGPicture *pic = GetPicture(name);
139 if (!pic)
140 pic = &fEmptyPic;
141 return pic;
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Get picture with specified size from pool (picture will be scaled if
146/// necessary). Picture must be freed using TGPicturePool::FreePicture(). If
147/// picture is not found 0 is returned.
148
151{
152 if (!fPicList)
153 fPicList = new THashTable(50);
154
156 pname.Strip();
157 TString ext = strrchr(pname, '.');
158 ext.ToLower();
159
160 if (ext.Length()) { // ".xpm", ".gif" etc
163 }
164
167 if (pic && pic->GetWidth() == new_width && pic->GetHeight() == new_height) {
168 if (pic->fPic == kNone)
169 return 0;
170 pic->AddReference();
171 return pic;
172 }
173
175 if (!picnam) {
176 pic = new TGPicture(hname, kTRUE);
177 pic->fAttributes.fColormap = fClient->GetDefaultColormap();
178 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
179 pic->fAttributes.fMask = kPASize | kPAColormap | kPACloseness;
180 pic->fAttributes.fWidth = new_width;
181 pic->fAttributes.fHeight = new_height;
182 fPicList->Add(pic);
183 return 0;
184 }
185
187 if (!img) {
188 pic = new TGPicture(hname, kTRUE);
189 pic->fAttributes.fColormap = fClient->GetDefaultColormap();
190 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
191 pic->fAttributes.fMask = kPASize | kPAColormap | kPACloseness;
192 pic->fAttributes.fWidth = new_width;
193 pic->fAttributes.fHeight = new_height;
194 fPicList->Add(pic);
195 delete [] picnam;
196 return 0;
197 }
198
199 img->Scale(new_width, new_height);
200
201 pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
202 delete [] picnam;
203 delete img;
204 fPicList->Add(pic);
205 return pic;
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// Get picture with specified pixmap and mask from pool.
210/// Picture must be freed using TGPicturePool::FreePicture().
211/// If picture is not found 0 is returned.
212
215{
216 if (!fPicList)
217 fPicList = new THashTable(50);
218
219 Int_t xy;
220 UInt_t w, h;
221
222 gVirtualX->GetWindowSize(pxmap, xy, xy, w, h);
223
224 const char *hname = TGPicture::HashName(name, w, h);
226
227 if (pic) {
228 pic->AddReference();
229 return pic;
230 }
231
232 pic = new TGPicture(hname, pxmap, mask);
233 fPicList->Add(pic);
234
235 return pic;
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Create picture from XPM data.
240/// Picture must be freed using TGPicturePool::FreePicture().
241/// If picture creation failed 0 is returned.
242
243const TGPicture *TGPicturePool::GetPicture(const char *name, char **xpm)
244{
245 UInt_t w, h;
246
247 if (!xpm || !*xpm) {
248 return 0;
249 }
250
251 if (!fPicList) {
252 fPicList = new THashTable(50);
253 }
254 char *ptr = xpm[0];
255 while (isspace((int)*ptr)) ++ptr;
256 w = atoi(ptr);
257
258 while (isspace((int)*ptr)) ++ptr;
259 h = atoi(ptr);
260
261 const char *hname = TGPicture::HashName(name, w, h);
263 if (pic) {
264 pic->AddReference();
265 return pic;
266 }
267
269 if (!img) {
270 pic = new TGPicture(hname, kTRUE);
271 pic->fAttributes.fColormap = fClient->GetDefaultColormap();
272 pic->fAttributes.fCloseness = 40000; // Allow for "similar" colors
273 pic->fAttributes.fMask = kPASize | kPAColormap | kPACloseness;
274 pic->fAttributes.fWidth = w;
275 pic->fAttributes.fHeight = h;
276 fPicList->Add(pic);
277 return 0;
278 }
279
280 pic = new TGPicture(hname, img->GetPixmap(), img->GetMask());
281 delete img;
282 return pic;
283}
284
285////////////////////////////////////////////////////////////////////////////////
286/// Remove picture from cache if nobody is using it anymore.
287
289{
290 if (!fPicList) return;
291
293 if (pic) {
294 if (pic->RemoveReference() == 0) {
296 delete pic;
297 }
298 }
299}
300
301////////////////////////////////////////////////////////////////////////////////
302/// Delete picture cache.
303
305{
306 if (fPicList) {
307 fPicList->Delete();
308 delete fPicList;
309 }
310
311 // Required since we overload TObject::Hash.
313}
314
315////////////////////////////////////////////////////////////////////////////////
316/// List all pictures in the pool.
317
319{
320 if (fPicList)
321 fPicList->Print();
322 else
323 Info("Print", "no pictures in picture pool");
324}
325
326////////////////////////////////////////////////////////////////////////////////
327/// ctor. Important: both pixmaps pxmap and mask must be unique (not shared)
328
330{
331 fName = name;
332 fScaled = kFALSE;
333 fPic = pxmap;
334 fMask = mask;
335 Int_t xy;
336
337 fAttributes.fColormap = gClient->GetDefaultColormap();
338 fAttributes.fCloseness = 40000; // Allow for "similar" colors
345
347 SetRefCount(1);
348}
349
350////////////////////////////////////////////////////////////////////////////////
351/// Draw a picture.
352
354{
356
358 gcv.fClipMask = fMask;
359 gcv.fClipXOrigin = x;
360 gcv.fClipYOrigin = y;
361 gVirtualX->ChangeGC(gc, &gcv);
362 gVirtualX->CopyArea(fPic, id, gc, 0, 0, fAttributes.fWidth, fAttributes.fHeight,
363 x, y);
364 gcv.fMask = kGCClipMask;
365 gcv.fClipMask = kNone;
366 gVirtualX->ChangeGC(gc, &gcv);
367}
368
369////////////////////////////////////////////////////////////////////////////////
370/// Delete picture object.
371
373{
374 if (fPic != kNone)
375 gVirtualX->DeletePixmap(fPic);
376 if (fMask != kNone)
377 gVirtualX->DeletePixmap(fMask);
379 delete [] fAttributes.fPixels;
380}
381
382////////////////////////////////////////////////////////////////////////////////
383/// Static function returning a unique name used to look up a picture.
384/// The unique name has the form "name__widthxheight".
385
386const char *TGPicture::HashName(const char *name, Int_t width, Int_t height)
387{
388 static TString hashName;
389
390 hashName.Form("%s__%dx%d", name, width, height);
391 return hashName.Data();
392}
393
394////////////////////////////////////////////////////////////////////////////////
395/// Print picture info.
396
398{
399 Printf("TGPicture: %s,%sref cnt = %u %lx", GetName(),
400 fScaled ? " scaled, " : " ", References(), fPic);
401}
402
403
404////////////////////////////////////////////////////////////////////////////////
405/// Create a "selected" looking picture based on the original TGPicture.
406
408 TGPicture("")
409{
411 UInt_t w, h;
412
413 fClient = client;
414 Window_t root = fClient->GetDefaultRoot()->GetId();
415
416 w = p->GetWidth();
417 h = p->GetHeight();
418
419 fPic = gVirtualX->CreatePixmap(root, w, h);
420 fMask = p->GetMask();
421
424
425 gVirtualX->CopyArea(p->GetPicture(), fPic, GetSelectedGC()(), 0, 0, w, h, 0, 0);
426
428 gcv.fClipMask = p->GetMask();
429 gcv.fClipXOrigin = 0;
430 gcv.fClipYOrigin = 0;
431 GetSelectedGC().SetAttributes(&gcv);
432
433 gVirtualX->FillRectangle(fPic, GetSelectedGC()(), 0, 0, w, h);
434
435 GetSelectedGC().SetClipMask(kNone);
436}
437
438////////////////////////////////////////////////////////////////////////////////
439/// Delete selected picture.
440
442{
443 // fMask was borrowed so should not be deleted by ~TGPicture.
444 fMask = kNone;
445}
446
447////////////////////////////////////////////////////////////////////////////////
448/// Return selection graphics context in use.
449
451{
452 if (!fgSelectedGC) {
453 fgSelectedGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
454 fgSelectedGC->SetForeground(gClient->GetResourcePool()->GetSelectedBgndColor());
455 fgSelectedGC->SetBackground(gClient->GetResourcePool()->GetBlackColor());
456 fgSelectedGC->SetFillStyle(kFillStippled);
457 fgSelectedGC->SetStipple(gClient->GetResourcePool()->GetCheckeredBitmap());
458 }
459 return *fgSelectedGC;
460}
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:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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:2509
@ kReadPermission
Definition TSystem.h:55
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
#define gVirtualX
Definition TVirtualX.h:337
Window client.
Definition TGClient.h:37
Encapsulate a graphics context used in the low level graphics.
Definition TGGC.h:22
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:53
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:80
TGPicturePool & operator=(const TGPicturePool &)
assignment operator
Definition TGPicture.cxx:64
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
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
~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
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:117
Mother of all ROOT objects.
Definition TObject.h:41
TObject & operator=(const TObject &rhs) noexcept
TObject assignment operator.
Definition TObject.h:299
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1045
void SetRefCount(UInt_t r)
Definition TRefCnt.h:39
UInt_t References() const
Definition TRefCnt.h:38
Basic string class.
Definition TString.h:138
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1285
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1073
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition TSystem.cxx:1559
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:400
Graphics context structure.
Definition GuiTypes.h:224
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