Logo ROOT   6.12/07
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 = 0;
50  char *path;
51 
52  if (!image)
53  image = "bld_rgb.xpm";
54 
55  path = StrDup(image);
56 
57  fPath = gSystem->DirName(path);
58 
59  fImage = TImage::Open(path);
60  if (fImage) {
64  }
65  SetWindowName();
66  delete [] path;
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Delete icon and free picture.
71 
73 {
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Set icon picture.
79 
80 void TGIcon::SetPicture(const TGPicture *pic)
81 {
82  fPic = pic;
83  gVirtualX->ClearWindow(fId);
84  fClient->NeedRedraw(this);
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Set icon image.
89 
90 void TGIcon::SetImage(const char *img)
91 {
92  //delete fImage;
93  TImage *i = TImage::Open(img);
94  fPath = gSystem->DirName(img);
95 
96  SetImage(i);
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// Change icon image.
101 
103 {
104  if (!img) {
105  return;
106  }
107 
108  delete fImage; // !! mem.leak!!
109  fImage = img;
110 
112  fClient->NeedRedraw(this);
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Return size of icon.
117 
119 {
120  return TGDimension((fPic) ? fPic->GetWidth() : fWidth,
121  (fPic) ? fPic->GetHeight() : fHeight);
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Redraw picture.
126 
128 {
129  Bool_t border = (GetOptions() & kRaisedFrame) ||
130  (GetOptions() & kSunkenFrame) ||
132 
133  if (fPic) fPic->Draw(fId, GetBckgndGC()(), border, border);
134  if (border) DrawBorder();
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// Resize.
139 
141 {
142  TGFrame::Resize(w, h);
143 
144  // allow scaled resize for icons with TImage
145  if (!fImage) {
146  return;
147  }
148 
149  gVirtualX->ClearWindow(fId);
150 
151  if (fPic) {
153  }
154  Bool_t border = (GetOptions() & kRaisedFrame) ||
155  (GetOptions() & kSunkenFrame) ||
157 
158  fImage->Scale(w - 2*border, h - 2*border);
160  fImage->GetPixmap(), fImage->GetMask());
161  DoRedraw();
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Move icon to (x,y) and resize it to (w,h).
166 
168 {
169  Move(x, y);
170  Resize(w, h);
171 }
172 
173 ////////////////////////////////////////////////////////////////////////////////
174 /// Reset icon to original image. It can be used only via context menu.
175 
177 {
178  if (!fImage || !fClient->IsEditable()) return;
179 
180  TString name = fImage->GetName();
181  name.Chop();
182  char *path = gSystem->ConcatFileName(fPath.Data(), name.Data());
183  SetImage(path);
184 
185  delete [] path;
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// Set directory where image is located
190 
191 void TGIcon::SetImagePath(const char *path)
192 {
193  if (!path) {
194  return;
195  }
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  const char *picname = fPic->GetName();
214 
215  out <<" TGIcon *";
216  if (!fImage) {
217  out << GetName() << " = new TGIcon(" << fParent->GetName()
218  << ",gClient->GetPicture(" << quote
219  << gSystem->ExpandPathName(gSystem->UnixPathName(picname)) // if no path
220  << quote << ")" << "," << GetWidth() << "," << GetHeight();
222  if (!GetOptions()) {
223  out <<");" << std::endl;
224  } else {
225  out << "," << GetOptionString() <<");" << std::endl;
226  }
227  } else {
228  out << "," << GetOptionString() << ",ucolor);" << std::endl;
229  }
230  } else {
231  TString name = fPath;
232  name += "/";
233  name += fImage->GetName();
234  name.Chop();
235  out << GetName() << " = new TGIcon(" << fParent->GetName() << ","
236  << quote << name.Data() << quote << ");" << std::endl;
237  }
238  if (option && strstr(option, "keep_names"))
239  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
240 }
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:932
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
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
TH1 * h
Definition: legend2.C:5
virtual void SetImage(const char *img)
Set icon image.
Definition: TGIcon.cxx:90
virtual void DrawBorder()
Draw frame border.
Definition: TGFrame.cxx:403
Basic string class.
Definition: TString.h:125
Pixel_t fBackground
Definition: TGFrame.h:142
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1004
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:140
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 Unix pathname to a local pathname.
Definition: TSystem.cxx:1044
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:306
Definition: TGIcon.h:30
virtual ~TGIcon()
Delete icon and free picture.
Definition: TGIcon.cxx:72
virtual TGDimension GetDefaultSize() const
Return size of icon.
Definition: TGIcon.cxx:118
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:167
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
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
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:350
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:2544
virtual void SetImagePath(const char *path)
Set directory where image is located.
Definition: TGIcon.cxx:191
#define ClassImp(name)
Definition: Rtypes.h:359
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:127
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:370
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2433
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2460
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition: TImage.cxx:110
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1254
virtual char * ConcatFileName(const char *dir, const char *name)
Concatenate a directory and a file name. User must delete returned string.
Definition: TSystem.cxx:1052
virtual void SetPicture(const TGPicture *pic)
Set icon picture.
Definition: TGIcon.cxx:80
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
TString & Chop()
Definition: TString.h:625
virtual void Reset()
Reset icon to original image. It can be used only via context menu.
Definition: TGIcon.cxx:176
const char * Data() const
Definition: TString.h:345