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 "Riostream.h"
36#include "TMath.h"
37#include "TGFileDialog.h"
38#include "TGMsgBox.h"
39#include "TVirtualDragManager.h"
40#include "TVirtualX.h"
41
42
44
45////////////////////////////////////////////////////////////////////////////////
46/// Create icon.
47
48TGIcon::TGIcon(const TGWindow *p, const char *image) : TGFrame(p, 1, 1)
49{
50 fPic = nullptr;
51
52 if (!image)
53 image = "bld_rgb.xpm";
54
55 char *path = StrDup(image);
56
57 fPath = gSystem->GetDirName(path);
58
59 fImage = TImage::Open(path);
60 if (fImage) {
64 }
66 delete [] path;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Delete icon and free picture.
71
73{
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Set icon picture.
79
81{
82 fPic = pic;
83 gVirtualX->ClearWindow(fId);
84 fClient->NeedRedraw(this);
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Set icon image.
89
90void TGIcon::SetImage(const char *img)
91{
92 //delete fImage;
93 TImage *i = TImage::Open(img);
94 fPath = gSystem->GetDirName(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);
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
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
191void TGIcon::SetImagePath(const char *path)
192{
193 if (!path) {
194 return;
195 }
196
197 fPath = gSystem->UnixPathName(path);
199}
200
201////////////////////////////////////////////////////////////////////////////////
202/// Save an icon widget as a C++ statement(s) on output stream out.
203
204void TGIcon::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
205{
206 char quote = '"';
207
209
210 if (!fPic) {
211 Error("SavePrimitive()", "icon pixmap not found ");
212 return;
213 }
214
215 TString picname = gSystem->UnixPathName(fPic->GetName());
216 gSystem->ExpandPathName(picname);
217
218 out <<" TGIcon *";
219 if (!fImage) {
220 out << GetName() << " = new TGIcon(" << fParent->GetName()
221 << ",gClient->GetPicture(" << quote
222 << picname // if no path
223 << quote << ")" << "," << GetWidth() << "," << GetHeight();
225 if (!GetOptions()) {
226 out <<");" << std::endl;
227 } else {
228 out << "," << GetOptionString() <<");" << std::endl;
229 }
230 } else {
231 out << "," << GetOptionString() << ",ucolor);" << std::endl;
232 }
233 } else {
235 name += "/";
236 name += fImage->GetName();
237 name.Chop();
238 out << GetName() << " = new TGIcon(" << fParent->GetName() << ","
239 << quote << name.Data() << quote << ");" << std::endl;
240 }
241 if (option && strstr(option, "keep_names"))
242 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
243}
@ kRaisedFrame
Definition: GuiTypes.h:384
@ kSunkenFrame
Definition: GuiTypes.h:383
@ kDoubleBorder
Definition: GuiTypes.h:385
#define h(i)
Definition: RSha256.hxx:106
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
char name[80]
Definition: TGX11.cxx:109
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2490
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
#define gVirtualX
Definition: TVirtualX.h:338
Bool_t IsEditable() const
Definition: TGClient.h:98
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition: TGClient.cxx:372
TGPicturePool * GetPicturePool() const
Definition: TGClient.h:135
void FreePicture(const TGPicture *pic)
Free picture resource.
Definition: TGClient.cxx:308
UInt_t fHeight
Definition: TGFrame.h:113
virtual void DrawBorder()
Draw frame border.
Definition: TGFrame.cxx:405
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:667
virtual UInt_t GetOptions() const
Definition: TGFrame.h:222
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2464
virtual void Move(Int_t x, Int_t y)
Move frame.
Definition: TGFrame.cxx:577
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:589
UInt_t fWidth
Definition: TGFrame.h:112
UInt_t GetHeight() const
Definition: TGFrame.h:250
UInt_t GetWidth() const
Definition: TGFrame.h:249
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2437
Pixel_t fBackground
Definition: TGFrame.h:120
static const TGGC & GetBckgndGC()
Get background color graphics context.
Definition: TGFrame.cxx:759
Definition: TGIcon.h:30
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
virtual void DoRedraw()
Redraw picture.
Definition: TGIcon.cxx:127
virtual ~TGIcon()
Delete icon and free picture.
Definition: TGIcon.cxx:72
virtual void SetPicture(const TGPicture *pic)
Set icon picture.
Definition: TGIcon.cxx:80
virtual void SetImage(const char *img)
Set icon image.
Definition: TGIcon.cxx:90
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:204
const TGPicture * fPic
Definition: TGIcon.h:33
TImage * fImage
Definition: TGIcon.h:34
virtual TGDimension GetDefaultSize() const
Return size of icon.
Definition: TGIcon.cxx:118
TString fPath
Definition: TGIcon.h:35
TGIcon(const TGIcon &)
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize.
Definition: TGIcon.cxx:140
virtual void Reset()
Reset icon to original image. It can be used only via context menu.
Definition: TGIcon.cxx:176
virtual void SetImagePath(const char *path)
Set directory where image is located.
Definition: TGIcon.cxx:191
TGClient * fClient
Definition: TGObject.h:37
Handle_t fId
Definition: TGObject.h:36
const TGPicture * GetPicture(const char *name)
Get a picture from the picture pool.
Definition: TGPicture.cxx:80
const char * GetName() const
Returns name of object.
Definition: TGPicture.h:62
UInt_t GetHeight() const
Definition: TGPicture.h:64
void Draw(Option_t *="")
Default Draw method for all objects.
Definition: TGPicture.h:57
UInt_t GetWidth() const
Definition: TGPicture.h:63
virtual void SetWindowName(const char *name=0)
Set window name.
Definition: TGWindow.cxx:119
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:326
const TGWindow * fParent
Definition: TGWindow.h:36
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:119
virtual UInt_t GetWidth() const
Definition: TImage.h:228
virtual void Scale(UInt_t, UInt_t)
Definition: TImage.h:141
virtual Pixmap_t GetPixmap()
Definition: TImage.h:235
virtual Pixmap_t GetMask()
Definition: TImage.h:236
virtual UInt_t GetHeight() const
Definition: TImage.h:229
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1269
virtual char * ConcatFileName(const char *dir, const char *name)
Concatenate a directory and a file name. User must delete returned string.
Definition: TSystem.cxx:1066
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1058
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:930
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1027
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17