Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGFSComboBox.cxx
Go to the documentation of this file.
1// @(#)root/gui:$Id$
2// Author: Fons Rademakers 19/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// TGFSComboBox, TGTreeLBEntry //
26// //
27// This is a combo box that is used in the File Selection dialog box. //
28// It will allow the file path selection. //
29// //
30//////////////////////////////////////////////////////////////////////////
31
32#include "RConfigure.h"
33
34#include "TGFSComboBox.h"
35#include "TGResourcePool.h"
36#include "TGPicture.h"
37#include "TSystem.h"
38#include "TVirtualX.h"
39#include "strlcpy.h"
40
41#include <cstdlib>
42#include <iostream>
43
46
47//--- this is temp here...
48
51
52////////////////////////////////////////////////////////////////////////////////
53/// Create a tree (i.e. entry can be indented) listbox entry.
54/// The strings text and path are adopted by the entry.
55
57 const TGPicture *pic, Int_t id, TGString *path,
58 GContext_t norm, FontStruct_t font, UInt_t options,
59 ULong_t back) :
60 TGLBEntry(p, id, options, back)
61{
62 if (text && !pic)
63 Error("TGTreeLBEntry", "icon not found for entry %s", text->GetString());
64
65 fPic = pic;
66 fSelPic = 0;
67 fTWidth = 0;
68 fText = text;
69 fPath = path;
70
71 fNormGC = norm;
72 fFontStruct = font;
73
75
76 int max_ascent, max_descent;
77 if (fText)
79 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
80 fTHeight = max_ascent + max_descent;
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// Delete tree listbox entry.
86
88{
89 delete fText;
90 delete fPath;
91 delete fSelPic;
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// Make entry active (highlight picture).
96
98{
99 if (fActive == a) return;
100 fActive = a;
101
102 if (fActive) {
104 } else {
105 if (fSelPic) delete fSelPic;
106 fSelPic = 0;
107 }
108 DoRedraw();
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Redraw the tree listbox entry on pixmap/window.
113
115{
116 int ix, iy, lx, ly;
117
118 ix = x;
119 iy = y + ((fHeight - fPic->GetHeight()) >> 1);
120 lx = x + (int)(fPic->GetWidth() + 4);
121 ly = y + (int)((fHeight - (fTHeight+1)) >> 1);
122
123 if (fActive) {
124 if (fSelPic) fSelPic->Draw(id, fNormGC, ix, iy);
126 gVirtualX->FillRectangle(id, fNormGC, lx-2, ly, fWidth-(lx-x), fTHeight+1);
128 } else {
129 fPic->Draw(id, fNormGC, ix, iy);
130 gVirtualX->SetForeground(fNormGC, fgWhitePixel);
131 gVirtualX->FillRectangle(id, fNormGC, lx-2, ly, fWidth-(lx-x), fTHeight+1);
132 gVirtualX->SetForeground(fNormGC, fgBlackPixel);
133 }
134
135 int max_ascent, max_descent;
136 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
137
138 fText->Draw(id, fNormGC, lx, ly + max_ascent);
139}
140
141////////////////////////////////////////////////////////////////////////////////
142/// Redraw the tree listbox entry.
143
145{
146 DrawCopy(fId, 0, 0);
147}
148
149////////////////////////////////////////////////////////////////////////////////
150/// Return default size of tree listbox entry.
151
153{
154 TGDimension isize(fPic->GetWidth(), fPic->GetHeight());
155 TGDimension lsize(fTWidth, fTHeight+1);
156
157 return TGDimension(isize.fWidth + lsize.fWidth + 4,
158 TMath::Max(isize.fHeight, lsize.fHeight) + 2);
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Update text and picture of a listbox entry.
163
165{
166 TGTreeLBEntry *te = (TGTreeLBEntry *) e;
167
168 if (fText) delete fText;
169 fText = new TGString(te->GetText());
170 fPic = te->GetPicture();
172 gVirtualX->ClearWindow(fId);
173 fClient->NeedRedraw(this);
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// Return default font structure.
178
180{
181 if (!fgDefaultFont)
182 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Return default graphics context.
188
190{
191 if (!fgDefaultGC)
192 fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
193 return *fgDefaultGC;
194}
195
196
197////////////////////////////////////////////////////////////////////////////////
198/// Create a file system combobox showing system directories.
199
201 ULong_t back) :
202 TGComboBox(parent, id, options | kOwnBackground, back)
203{
204 SetTopEntry(new TGTreeLBEntry(this, new TGString("Current dir"),
205 fClient->GetPicture("folder_t.xpm"), 0),
206 new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 4, 0, 0, 0));
207
210
211 //--- first check for the existence of some directories...
212
213 const char *homeDir = gSystem->HomeDirectory();
214#ifndef ROOTPREFIX
215 const char *rootSys = gSystem->Getenv("ROOTSYS");
216#else
217 // const char *rootSys = ROOTPREFIX;
218#endif
219
220 TList *volumes = gSystem->GetVolumes("all");
221 TList *curvol = gSystem->GetVolumes("cur");
222 const char *curdrive = "";
223 if (volumes && curvol) {
224 TNamed *named = (TNamed *)curvol->At(0);
225 if (named) {
226 curdrive = named->GetName();
227 TString infos = named->GetTitle();
228
229 fLbc.emplace_back(infos.Data(), Form("%s\\", curdrive), "hdisk_t.xpm", 0);
230 if (infos.Contains("Removable"))
231 fLbc.back().fPixmap = "fdisk_t.xpm";
232 else if (infos.Contains("Local"))
233 fLbc.back().fPixmap = "hdisk_t.xpm";
234 else if (infos.Contains("CD"))
235 fLbc.back().fPixmap = "cdrom_t.xpm";
236 else if (infos.Contains("Network"))
237 fLbc.back().fPixmap = "netdisk_t.xpm";
238 } else {
239 fLbc.emplace_back("Root", "/", "hdisk_t.xpm", 1);
240 }
241 } else {
242 fLbc.emplace_back("Root", "/", "hdisk_t.xpm", 1);
243 fLbc.emplace_back("Floppy", "/floppy", "fdisk_t.xpm", 1);
244 fLbc.emplace_back("CD-ROM", "/cdrom", "cdrom_t.xpm", 1);
245 }
246 fLbc.emplace_back("Home", "$HOME", "home_t.xpm", 1);
247#ifndef ROOTPREFIX
248 fLbc.emplace_back("RootSys", "$ROOTSYS", "root_t.xpm", 1);
249#else
250 fLbc.emplace_back(ROOTPREFIX, ROOTPREFIX, "root_t.xpm", 1);
251#endif
252
253 if (volumes && curvol) {
254 TIter next(volumes);
255 TNamed *drive;
256 while ((drive = (TNamed *)next())) {
257 if (!strcmp(drive->GetName(), curdrive))
258 continue;
259 TString infos = drive->GetTitle();
260
261 fLbc.emplace_back(drive->GetTitle(), Form("%s\\", drive->GetName()), "hdisk_t.xpm", 0);
262
263 if (infos.Contains("Removable"))
264 fLbc.back().fPixmap = "fdisk_t.xpm";
265 else if (infos.Contains("Local"))
266 fLbc.back().fPixmap = "hdisk_t.xpm";
267 else if (infos.Contains("CD"))
268 fLbc.back().fPixmap = "cdrom_t.xpm";
269 else if (infos.Contains("Network"))
270 fLbc.back().fPixmap = "netdisk_t.xpm";
271 }
272 delete volumes;
273 delete curvol;
274 }
275
276 Int_t cnt = 0;
277
278 for (auto &entry : fLbc) {
279 entry.fId = ++cnt * 1000; // assign ids after vector is created
280 if (entry.fPath.find("$HOME") == 0) {
281 if (homeDir) {
282 std::string newpath = homeDir;
283 newpath.append(entry.fPath.substr(5));
284 entry.fPath = newpath;
285 } else {
286 entry.fFlags = 0;
287 }
288 }
289#ifndef ROOTPREFIX
290 // Below should _only_ be called if the prefix isn't set at build
291 // time. The code below expands the occurance of `$ROOTSYS' in
292 // the table above. However, in the case of prefix being set at
293 // build time, we do not need to expand the prefix, as it is
294 // already known, so the entries in the table above are actually
295 // fully expanded.
296 if (entry.fPath.find("$ROOTSYS") == 0) {
297 // Get the size of the prefix template
298 const int plen = 8;
299 if (rootSys) {
300 std::string newpath = rootSys;
301 newpath.append(entry.fPath.substr(plen));
302 entry.fPath = newpath;
303 } else {
304 entry.fFlags = 0;
305 }
306 }
307#endif
308 if (gSystem->AccessPathName(entry.fPath.c_str(), kFileExists) == 0)
309 entry.fFlags = 1;
310 }
311
312 //--- then init the contents...
313
314 for (auto &entry : fLbc) {
315 if (entry.fFlags) {
316 int indent = 4 + (entry.fIndent * 10);
317 auto pic = fClient->GetPicture(entry.fPixmap.c_str());
318 if (!pic) Error("TGFSComboBox", "pixmap not found: %s", entry.fPixmap.c_str());
320 new TGString(entry.fName.c_str()), pic, entry.fId,
321 new TGString(entry.fPath.c_str())),
323 }
324 }
326}
327
328////////////////////////////////////////////////////////////////////////////////
329/// Update file system combo box.
330
331void TGFSComboBox::Update(const char *path)
332{
333 char dirname[1024], mpath[1024];
334 const char *tailpath = 0;
335 int indent_lvl = 0, afterID = -1, sel = -1;
336
337 if (!path) return;
338
339 for (int i = 0; i < (int) fLbc.size() - 1; ++i)
340 RemoveEntries(fLbc[i].fId+1, fLbc[i+1].fId-1);
341 RemoveEntries(fLbc.back().fId+1, (fLbc.size() + 1) * 1000 - 1);
342
343 int len = 0;
344 for (auto &entry : fLbc) {
345 if (entry.fFlags) {
346 int slen = entry.fPath.length();
347 if (strncmp(path, entry.fPath.c_str(), slen) == 0) {
348 if (slen > len) {
349 sel = afterID = entry.fId;
350 indent_lvl = entry.fIndent + 1;
351 if ((len > 0) && ((path[slen] == '\\') || (path[slen] == '/') ||
352 (path[slen] == 0)))
353 tailpath = path + slen;
354 strlcpy(mpath, entry.fPath.c_str(), 1024);
355 len = slen;
356 }
357 }
358 }
359 }
360
361 if (tailpath && *tailpath) {
362 if ((*tailpath == '/') || (*tailpath == '\\')) ++tailpath;
363 if (*tailpath)
364 while (1) {
365 const char *picname;
366 const char *semi = strchr(tailpath, '/');
367 if (semi == 0) semi = strchr(tailpath, '\\');
368 if (semi == 0) {
369 strlcpy(dirname, tailpath, 1024);
370 picname = "ofolder_t.xpm";
371 } else {
372 strlcpy(dirname, tailpath, (semi-tailpath)+1);
373 picname = "folder_t.xpm";
374 }
375 if ((mpath[strlen(mpath)-1] != '/') &&
376 (mpath[strlen(mpath)-1] != '\\')) {
377 strlcat(mpath, "/", 1024-strlen(mpath));
378 }
379 strlcat(mpath, dirname, 1024-strlen(mpath));
380 int indent = 4 + (indent_lvl * 10);
381 const TGPicture *pic = fClient->GetPicture(picname);
382 if (!pic) Error("Update", "pixmap not found: %s", picname);
384 new TGString(dirname), pic, afterID+1,
385 new TGString(mpath)),
387 indent, 0, 0, 0),
388 afterID);
389 sel = ++afterID;
390 ++indent_lvl;
391 if (semi == 0) break;
392 tailpath = ++semi;
393 }
394 }
395 if (sel > 0) Select(sel);
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Save a file system combo box as a C++ statement(s) on output stream out.
400
401void TGFSComboBox::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
402{
403 if (fBackground != GetWhitePixel()) SaveUserColor(out, option);
404
405 out << std::endl << " // file system combo box" << std::endl;
406 out << " TGFSComboBox *";
407 out << GetName() << " = new TGFSComboBox(" << fParent->GetName()
408 << "," << fWidgetId;
409 if (fBackground == GetWhitePixel()) {
411 out <<");" << std::endl;
412 } else {
413 out << "," << GetOptionString() <<");" << std::endl;
414 }
415 } else {
416 out << "," << GetOptionString() << ",ucolor);" << std::endl;
417 }
418 if (option && strstr(option, "keep_names"))
419 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
420
421 out << " " << GetName() << "->Resize(" << GetWidth() << ","
422 << GetHeight() << ");" << std::endl;
423 out << " " << GetName() << "->Select(" << GetSelected() << ");" << std::endl;
424
425}
ULong_t Handle_t
Generic resource handle.
Definition GuiTypes.h:26
const Mask_t kButtonPressMask
Definition GuiTypes.h:161
const Mask_t kPointerMotionMask
Definition GuiTypes.h:163
@ kSunkenFrame
Definition GuiTypes.h:383
@ kDoubleBorder
Definition GuiTypes.h:385
@ kHorizontalFrame
Definition GuiTypes.h:382
@ kOwnBackground
Definition GuiTypes.h:391
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:38
const Mask_t kButtonReleaseMask
Definition GuiTypes.h:162
Handle_t FontStruct_t
Pointer to font structure.
Definition GuiTypes.h:39
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
const Bool_t kFALSE
Definition RtypesCore.h:92
unsigned long ULong_t
Definition RtypesCore.h:55
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
static void indent(ostringstream &buf, int indent_level)
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
#define gClient
Definition TGClient.h:166
@ kLHintsExpandY
Definition TGLayout.h:38
@ kLHintsTop
Definition TGLayout.h:34
@ kLHintsExpandX
Definition TGLayout.h:37
XFontStruct * id
Definition TGX11.cxx:109
char * Form(const char *fmt,...)
@ kFileExists
Definition TSystem.h:44
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
#define gVirtualX
Definition TVirtualX.h:338
const TGResourcePool * GetResourcePool() const
Definition TGClient.h:133
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition TGClient.cxx:288
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition TGClient.cxx:371
virtual Int_t GetSelected() const
Definition TGComboBox.h:134
virtual void AddEntry(TGString *s, Int_t id)
Definition TGComboBox.h:106
TGListBox * fListBox
Definition TGComboBox.h:78
virtual void SetTopEntry(TGLBEntry *e, TGLayoutHints *lh)
Set a new combo box value (normally update of text string in fSelEntry is done via fSelEntry::Update(...
virtual void RemoveEntries(Int_t from_ID, Int_t to_ID)
Definition TGComboBox.h:125
virtual void Select(Int_t id, Bool_t emit=kTRUE)
Make the selected item visible in the combo box window and emit signals according to the second param...
virtual void InsertEntry(TGString *s, Int_t id, Int_t afterID)
Definition TGComboBox.h:112
UInt_t fHeight
Definition TGDimension.h:30
UInt_t fWidth
Definition TGDimension.h:29
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a file system combo box as a C++ statement(s) on output stream out.
TGFSComboBox(const TGWindow *p=0, Int_t id=-1, UInt_t options=kHorizontalFrame|kSunkenFrame|kDoubleBorder, Pixel_t back=GetWhitePixel())
Create a file system combobox showing system directories.
std::vector< Lbc_t > fLbc
! list of default entries
virtual void Update(const char *path)
Update file system combo box.
FontStruct_t GetFontStruct() const
Definition TGFont.h:193
void AddInput(UInt_t emask)
Add events specified in the emask to the events the frame should handle.
Definition TGFrame.cxx:324
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition TGFrame.cxx:694
UInt_t fHeight
Definition TGFrame.h:112
static Pixel_t fgDefaultSelectedBackground
Definition TGFrame.h:126
virtual UInt_t GetOptions() const
Definition TGFrame.h:221
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition TGFrame.cxx:2465
UInt_t fWidth
Definition TGFrame.h:111
UInt_t GetHeight() const
Definition TGFrame.h:249
static Pixel_t fgWhitePixel
Definition TGFrame.h:127
UInt_t GetWidth() const
Definition TGFrame.h:248
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition TGFrame.cxx:2438
Pixel_t fBackground
Definition TGFrame.h:119
static Pixel_t fgBlackPixel
Definition TGFrame.h:128
Definition TGGC.h:31
virtual TGFrame * GetContainer() const
Definition TGListBox.h:334
TGClient * fClient
Definition TGObject.h:37
Handle_t fId
Definition TGObject.h:36
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
Pixel_t GetSelectedFgndColor() const
Int_t GetLength() const
Definition TGString.h:39
const char * GetString() const
Definition TGString.h:40
virtual void Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
Draw string.
Definition TGString.cxx:51
virtual void Activate(Bool_t a)
Make entry active (highlight picture).
static const TGFont * fgDefaultFont
virtual ~TGTreeLBEntry()
Delete tree listbox entry.
static TGGC * fgDefaultGC
virtual TGDimension GetDefaultSize() const
Return default size of tree listbox entry.
virtual void DoRedraw()
Redraw the tree listbox entry.
const TGPicture * GetPicture() const
TGTreeLBEntry(const TGWindow *p=0, TGString *text=0, const TGPicture *pic=0, Int_t id=-1, TGString *path=0, GContext_t norm=GetDefaultGC()(), FontStruct_t font=GetDefaultFontStruct(), UInt_t options=kHorizontalFrame, Pixel_t back=GetWhitePixel())
Create a tree (i.e.
const TGPicture * fPic
FontStruct_t fFontStruct
TGString * fPath
virtual void Update(TGLBEntry *e)
Update text and picture of a listbox entry.
static const TGGC & GetDefaultGC()
Return default graphics context.
const TGString * GetText() const
virtual void DrawCopy(Handle_t id, Int_t x, Int_t y)
Redraw the tree listbox entry on pixmap/window.
GContext_t fNormGC
TGSelectedPicture * fSelPic
TGString * fText
static FontStruct_t GetDefaultFontStruct()
Return default font structure.
Int_t fWidgetId
Definition TGWidget.h:56
virtual void SetWindowName(const char *name=0)
Set window name.
Definition TGWindow.cxx:128
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition TGWindow.cxx:335
const TGWindow * fParent
Definition TGWindow.h:36
A doubly linked list.
Definition TList.h:44
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:357
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1661
virtual TList * GetVolumes(Option_t *) const
Definition TSystem.h:453
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1294
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:886
TText * text
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Short_t Max(Short_t a, Short_t b)
Definition TMathBase.h:212