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