Logo ROOT  
Reference Guide
TGIcon.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 05/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 // TGIcon //
26 // //
27 // This class handles GUI icons. //
28 // //
29 //////////////////////////////////////////////////////////////////////////
30 
31 #include "TGIcon.h"
32 #include "TGPicture.h"
33 #include "TSystem.h"
34 #include "TImage.h"
35 #include "TVirtualDragManager.h"
36 #include "TVirtualX.h"
37 
38 #include <iostream>
39 
40 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Create icon.
45 
46 TGIcon::TGIcon(const TGWindow *p, const char *image) : TGFrame(p, 1, 1)
47 {
48  fPic = nullptr;
49 
50  if (!image)
51  image = "bld_rgb.xpm";
52 
53  char *path = StrDup(image);
54 
55  fPath = gSystem->GetDirName(path);
56 
57  fImage = TImage::Open(path);
58  if (fImage) {
62  }
63  SetWindowName();
64  delete [] path;
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Delete icon and free picture.
69 
71 {
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Set icon picture.
77 
78 void TGIcon::SetPicture(const TGPicture *pic)
79 {
80  fPic = pic;
81  gVirtualX->ClearWindow(fId);
82  fClient->NeedRedraw(this);
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Set icon image.
87 
88 void TGIcon::SetImage(const char *img)
89 {
90  //delete fImage;
91  TImage *i = TImage::Open(img);
92  fPath = gSystem->GetDirName(img);
93 
94  SetImage(i);
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Change icon image.
99 
101 {
102  if (!img) {
103  return;
104  }
105 
106  delete fImage; // !! mem.leak!!
107  fImage = img;
108 
110  fClient->NeedRedraw(this);
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Return size of icon.
115 
117 {
118  return TGDimension((fPic) ? fPic->GetWidth() : fWidth,
119  (fPic) ? fPic->GetHeight() : fHeight);
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Redraw picture.
124 
126 {
127  Bool_t border = (GetOptions() & kRaisedFrame) ||
128  (GetOptions() & kSunkenFrame) ||
130 
131  if (fPic) fPic->Draw(fId, GetBckgndGC()(), border, border);
132  if (border) DrawBorder();
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Resize.
137 
139 {
140  TGFrame::Resize(w, h);
141 
142  // allow scaled resize for icons with TImage
143  if (!fImage) {
144  return;
145  }
146 
147  gVirtualX->ClearWindow(fId);
148 
149  if (fPic) {
151  }
152  Bool_t border = (GetOptions() & kRaisedFrame) ||
153  (GetOptions() & kSunkenFrame) ||
155 
156  fImage->Scale(w - 2*border, h - 2*border);
158  fImage->GetPixmap(), fImage->GetMask());
159  DoRedraw();
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// Move icon to (x,y) and resize it to (w,h).
164 
166 {
167  Move(x, y);
168  Resize(w, h);
169 }
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Reset icon to original image. It can be used only via context menu.
173 
175 {
176  if (!fImage || !fClient->IsEditable()) return;
177 
178  TString name = fImage->GetName();
179  name.Chop();
180  char *path = gSystem->ConcatFileName(fPath.Data(), name.Data());
181  SetImage(path);
182 
183  delete [] path;
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Set directory where image is located
188 
189 void TGIcon::SetImagePath(const char *path)
190 {
191  if (!path) {
192  return;
193  }
194 
195  fPath = gSystem->UnixPathName(path);
197 }
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Save an icon widget as a C++ statement(s) on output stream out.
201 
202 void TGIcon::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
203 {
204  char quote = '"';
205 
206  if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
207 
208  if (!fPic) {
209  Error("SavePrimitive()", "icon pixmap not found ");
210  return;
211  }
212 
213  TString picname = gSystem->UnixPathName(fPic->GetName());
214  gSystem->ExpandPathName(picname);
215 
216  out <<" TGIcon *";
217  if (!fImage) {
218  out << GetName() << " = new TGIcon(" << fParent->GetName()
219  << ",gClient->GetPicture(" << quote
220  << picname // if no path
221  << quote << ")" << "," << GetWidth() << "," << GetHeight();
223  if (!GetOptions()) {
224  out <<");" << std::endl;
225  } else {
226  out << "," << GetOptionString() <<");" << std::endl;
227  }
228  } else {
229  out << "," << GetOptionString() << ",ucolor);" << std::endl;
230  }
231  } else {
232  TString name = fPath;
233  name += "/";
234  name += fImage->GetName();
235  name.Chop();
236  out << GetName() << " = new TGIcon(" << fParent->GetName() << ","
237  << quote << name.Data() << quote << ");" << std::endl;
238  }
239  if (option && strstr(option, "keep_names"))
240  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
241 }
TGWindow::SetWindowName
virtual void SetWindowName(const char *name=0)
Set window name.
Definition: TGWindow.cxx:118
TGFrame::GetHeight
UInt_t GetHeight() const
Definition: TGFrame.h:250
TGWindow
Definition: TGWindow.h:31
TGIcon::Resize
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize.
Definition: TGIcon.cxx:138
TGPicture
Definition: TGPicture.h:36
TGDimension
Definition: TGDimension.h:27
TGIcon::SetPicture
virtual void SetPicture(const TGPicture *pic)
Set icon picture.
Definition: TGIcon.cxx:78
TGPicture::GetWidth
UInt_t GetWidth() const
Definition: TGPicture.h:63
TSystem::BaseName
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:933
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TGPicture.h
TGFrame::fWidth
UInt_t fWidth
Definition: TGFrame.h:112
TGIcon.h
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TGIcon::Reset
virtual void Reset()
Reset icon to original image. It can be used only via context menu.
Definition: TGIcon.cxx:174
TGClient::IsEditable
Bool_t IsEditable() const
Definition: TGClient.h:98
TGIcon::~TGIcon
virtual ~TGIcon()
Delete icon and free picture.
Definition: TGIcon.cxx:70
TImage
Definition: TImage.h:29
TGPicture::Draw
void Draw(Option_t *="")
Default Draw method for all objects.
Definition: TGPicture.h:57
x
Double_t x[n]
Definition: legend1.C:17
TImage::GetHeight
virtual UInt_t GetHeight() const
Definition: TImage.h:229
TGFrame::GetDefaultFrameBackground
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:668
TVirtualX.h
TGClient::FreePicture
void FreePicture(const TGPicture *pic)
Free picture resource.
Definition: TGClient.cxx:309
TGIcon::GetDefaultSize
virtual TGDimension GetDefaultSize() const
Return size of icon.
Definition: TGIcon.cxx:116
TImage::Open
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition: TImage.cxx:118
TString
Definition: TString.h:136
TGFrame::DrawBorder
virtual void DrawBorder()
Draw frame border.
Definition: TGFrame.cxx:406
TGFrame
Definition: TGFrame.h:105
kDoubleBorder
@ kDoubleBorder
Definition: GuiTypes.h:385
TGIcon::SetImage
virtual void SetImage(const char *img)
Set icon image.
Definition: TGIcon.cxx:88
TGClient::GetPicturePool
TGPicturePool * GetPicturePool() const
Definition: TGClient.h:135
TSystem::GetDirName
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1030
bool
TGFrame::GetWidth
UInt_t GetWidth() const
Definition: TGFrame.h:249
TImage.h
TGFrame::GetOptionString
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2465
TGFrame::GetOptions
virtual UInt_t GetOptions() const
Definition: TGFrame.h:222
TGIcon
Definition: TGIcon.h:30
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
kSunkenFrame
@ kSunkenFrame
Definition: GuiTypes.h:383
TGPicturePool::GetPicture
const TGPicture * GetPicture(const char *name)
Get a picture from the picture pool.
Definition: TGPicture.cxx:80
TGIcon::fPic
const TGPicture * fPic
Definition: TGIcon.h:33
TSystem.h
h
#define h(i)
Definition: RSha256.hxx:124
TImage::Scale
virtual void Scale(UInt_t, UInt_t)
Definition: TImage.h:141
TGWindow::GetName
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:317
TGFrame::Resize
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:590
TGObject::fId
Handle_t fId
Definition: TGObject.h:36
TImage::GetMask
virtual Pixmap_t GetMask()
Definition: TImage.h:236
TGFrame::SaveUserColor
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2438
StrDup
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2490
y
Double_t y[n]
Definition: legend1.C:17
TSystem::ExpandPathName
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1272
TGFrame::GetBckgndGC
static const TGGC & GetBckgndGC()
Get background color graphics context.
Definition: TGFrame.cxx:760
TGIcon::fPath
TString fPath
Definition: TGIcon.h:35
kRaisedFrame
@ kRaisedFrame
Definition: GuiTypes.h:384
unsigned int
TGIcon::MoveResize
virtual void MoveResize(Int_t x, Int_t y, UInt_t w=0, UInt_t h=0)
Move icon to (x,y) and resize it to (w,h).
Definition: TGIcon.cxx:165
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TGIcon::SavePrimitive
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save an icon widget as a C++ statement(s) on output stream out.
Definition: TGIcon.cxx:202
TImage::GetWidth
virtual UInt_t GetWidth() const
Definition: TImage.h:228
TGClient::NeedRedraw
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition: TGClient.cxx:373
TSystem::UnixPathName
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1061
TGIcon::TGIcon
TGIcon(const TGIcon &)
TGObject::fClient
TGClient * fClient
Definition: TGObject.h:37
TGIcon::fImage
TImage * fImage
Definition: TGIcon.h:34
TGFrame::Move
virtual void Move(Int_t x, Int_t y)
Move frame.
Definition: TGFrame.cxx:578
TImage::GetPixmap
virtual Pixmap_t GetPixmap()
Definition: TImage.h:235
name
char name[80]
Definition: TGX11.cxx:110
TGPicture::GetName
const char * GetName() const
Returns name of object.
Definition: TGPicture.h:62
TSystem::ConcatFileName
virtual char * ConcatFileName(const char *dir, const char *name)
Concatenate a directory and a file name. User must delete returned string.
Definition: TSystem.cxx:1069
TGIcon::SetImagePath
virtual void SetImagePath(const char *path)
Set directory where image is located.
Definition: TGIcon.cxx:189
TGFrame::fHeight
UInt_t fHeight
Definition: TGFrame.h:113
TGFrame::fBackground
Pixel_t fBackground
Definition: TGFrame.h:120
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TVirtualDragManager.h
TGPicture::GetHeight
UInt_t GetHeight() const
Definition: TGPicture.h:64
TGWindow::fParent
const TGWindow * fParent
Definition: TGWindow.h:36
int
TGIcon::DoRedraw
virtual void DoRedraw()
Redraw picture.
Definition: TGIcon.cxx:125