Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
42 ClassImp(TGIcon);
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) {
61  fPic = fClient->GetPicturePool()->GetPicture(gSystem->BaseName(path),
62  fImage->GetPixmap(), fImage->GetMask());
63  TGFrame::Resize(fImage->GetWidth(), fImage->GetHeight());
64  }
65  SetWindowName();
66  delete [] path;
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Delete icon and free picture.
71 
72 TGIcon::~TGIcon()
73 {
74  if (fPic) fClient->FreePicture(fPic);
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 
102 void TGIcon::SetImage(TImage *img)
103 {
104  if (!img) {
105  return;
106  }
107 
108  delete fImage; // !! mem.leak!!
109  fImage = img;
110 
111  Resize(fImage->GetWidth(), fImage->GetHeight());
112  fClient->NeedRedraw(this);
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Return size of icon.
117 
118 TGDimension TGIcon::GetDefaultSize() const
119 {
120  return TGDimension((fPic) ? fPic->GetWidth() : fWidth,
121  (fPic) ? fPic->GetHeight() : fHeight);
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Redraw picture.
126 
127 void TGIcon::DoRedraw()
128 {
129  Bool_t border = (GetOptions() & kRaisedFrame) ||
130  (GetOptions() & kSunkenFrame) ||
131  (GetOptions() & kDoubleBorder);
132 
133  if (fPic) fPic->Draw(fId, GetBckgndGC()(), border, border);
134  if (border) DrawBorder();
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// Resize.
139 
140 void TGIcon::Resize(UInt_t w, UInt_t h)
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) {
152  fClient->FreePicture(fPic);
153  }
154  Bool_t border = (GetOptions() & kRaisedFrame) ||
155  (GetOptions() & kSunkenFrame) ||
156  (GetOptions() & kDoubleBorder);
157 
158  fImage->Scale(w - 2*border, h - 2*border);
159  fPic = fClient->GetPicturePool()->GetPicture(fImage->GetName(),
160  fImage->GetPixmap(), fImage->GetMask());
161  DoRedraw();
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Move icon to (x,y) and resize it to (w,h).
166 
167 void TGIcon::MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h)
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 
176 void TGIcon::Reset()
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  }
196  fPath = gSystem->ExpandPathName(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  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();
221  if (fBackground == GetDefaultFrameBackground()) {
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 }