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/** \class TGFSComboBox
25 \ingroup guiwidgets
26
27This is a combo box that is used in the File Selection dialog box.
28It will allow the file path selection.
29
30*/
31
32
33#include "RConfigure.h"
34
35#include "TGFSComboBox.h"
36#include "TGResourcePool.h"
37#include "TGPicture.h"
38#include "TSystem.h"
39#include "TVirtualX.h"
40#include "strlcpy.h"
41
42#include <cstdlib>
43#include <iostream>
44
47
48//--- this is temp here...
49
52
53////////////////////////////////////////////////////////////////////////////////
54/// Create a tree (i.e. entry can be indented) listbox entry.
55/// The strings text and path are adopted by the entry.
56
58 const TGPicture *pic, Int_t id, TGString *path,
59 GContext_t norm, FontStruct_t font, UInt_t options,
60 ULong_t back) :
61 TGLBEntry(p, id, options, back)
62{
63 if (text && !pic)
64 Error("TGTreeLBEntry", "icon not found for entry %s", text->GetString());
65
66 fPic = pic;
67 fSelPic = 0;
68 fTWidth = 0;
69 fText = text;
70 fPath = path;
71
72 fNormGC = norm;
73 fFontStruct = font;
74
76
77 int max_ascent, max_descent;
78 if (fText)
80 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
81 fTHeight = max_ascent + max_descent;
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Delete tree listbox entry.
87
89{
90 delete fText;
91 delete fPath;
92 delete fSelPic;
93}
94
95////////////////////////////////////////////////////////////////////////////////
96/// Make entry active (highlight picture).
97
99{
100 if (fActive == a) return;
101 fActive = a;
102
103 if (fActive) {
105 } else {
106 if (fSelPic) delete fSelPic;
107 fSelPic = 0;
108 }
109 DoRedraw();
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Redraw the tree listbox entry on pixmap/window.
114
116{
117 int ix, iy, lx, ly;
118
119 ix = x;
120 iy = y + ((fHeight - fPic->GetHeight()) >> 1);
121 lx = x + (int)(fPic->GetWidth() + 4);
122 ly = y + (int)((fHeight - (fTHeight+1)) >> 1);
123
124 if (fActive) {
125 if (fSelPic) fSelPic->Draw(id, fNormGC, ix, iy);
127 gVirtualX->FillRectangle(id, fNormGC, lx-2, ly, fWidth-(lx-x), fTHeight+1);
129 } else {
130 fPic->Draw(id, fNormGC, ix, iy);
131 gVirtualX->SetForeground(fNormGC, fgWhitePixel);
132 gVirtualX->FillRectangle(id, fNormGC, lx-2, ly, fWidth-(lx-x), fTHeight+1);
133 gVirtualX->SetForeground(fNormGC, fgBlackPixel);
134 }
135
136 int max_ascent, max_descent;
137 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
138
139 fText->Draw(id, fNormGC, lx, ly + max_ascent);
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Redraw the tree listbox entry.
144
146{
147 DrawCopy(fId, 0, 0);
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Return default size of tree listbox entry.
152
154{
155 TGDimension isize(fPic->GetWidth(), fPic->GetHeight());
156 TGDimension lsize(fTWidth, fTHeight+1);
157
158 return TGDimension(isize.fWidth + lsize.fWidth + 4,
159 TMath::Max(isize.fHeight, lsize.fHeight) + 2);
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Update text and picture of a listbox entry.
164
166{
167 TGTreeLBEntry *te = (TGTreeLBEntry *) e;
168
169 if (fText) delete fText;
170 fText = new TGString(te->GetText());
171 fPic = te->GetPicture();
173 gVirtualX->ClearWindow(fId);
174 fClient->NeedRedraw(this);
175}
176
177////////////////////////////////////////////////////////////////////////////////
178/// Return default font structure.
179
181{
182 if (!fgDefaultFont)
183 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Return default graphics context.
189
191{
192 if (!fgDefaultGC)
193 fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
194 return *fgDefaultGC;
195}
196
197
198////////////////////////////////////////////////////////////////////////////////
199/// Create a file system combobox showing system directories.
200
202 ULong_t back) :
203 TGComboBox(parent, id, options | kOwnBackground, back)
204{
205 SetTopEntry(new TGTreeLBEntry(this, new TGString("Current dir"),
206 fClient->GetPicture("folder_t.xpm"), 0),
207 new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 4, 0, 0, 0));
208
211
212 //--- first check for the existence of some directories...
213
214 const char *homeDir = gSystem->HomeDirectory();
215#ifndef ROOTPREFIX
216 const char *rootSys = gSystem->Getenv("ROOTSYS");
217#else
218 // const char *rootSys = ROOTPREFIX;
219#endif
220
221 TList *volumes = gSystem->GetVolumes("all");
222 TList *curvol = gSystem->GetVolumes("cur");
223 const char *curdrive = "";
224 if (volumes && curvol) {
225 TNamed *named = (TNamed *)curvol->At(0);
226 if (named) {
227 curdrive = named->GetName();
228 TString infos = named->GetTitle();
229
230 fLbc.emplace_back(infos.Data(), Form("%s\\", curdrive), "hdisk_t.xpm", 0);
231 if (infos.Contains("Removable"))
232 fLbc.back().fPixmap = "fdisk_t.xpm";
233 else if (infos.Contains("Local"))
234 fLbc.back().fPixmap = "hdisk_t.xpm";
235 else if (infos.Contains("CD"))
236 fLbc.back().fPixmap = "cdrom_t.xpm";
237 else if (infos.Contains("Network"))
238 fLbc.back().fPixmap = "netdisk_t.xpm";
239 } else {
240 fLbc.emplace_back("Root", "/", "hdisk_t.xpm", 1);
241 }
242 } else {
243 fLbc.emplace_back("Root", "/", "hdisk_t.xpm", 1);
244 fLbc.emplace_back("Floppy", "/floppy", "fdisk_t.xpm", 1);
245 fLbc.emplace_back("CD-ROM", "/cdrom", "cdrom_t.xpm", 1);
246 }
247 fLbc.emplace_back("Home", "$HOME", "home_t.xpm", 1);
248#ifndef ROOTPREFIX
249 fLbc.emplace_back("RootSys", "$ROOTSYS", "root_t.xpm", 1);
250#else
251 fLbc.emplace_back(ROOTPREFIX, ROOTPREFIX, "root_t.xpm", 1);
252#endif
253
254 if (volumes && curvol) {
255 TIter next(volumes);
256 TNamed *drive;
257 while ((drive = (TNamed *)next())) {
258 if (!strcmp(drive->GetName(), curdrive))
259 continue;
260 TString infos = drive->GetTitle();
261
262 fLbc.emplace_back(drive->GetTitle(), Form("%s\\", drive->GetName()), "hdisk_t.xpm", 0);
263
264 if (infos.Contains("Removable"))
265 fLbc.back().fPixmap = "fdisk_t.xpm";
266 else if (infos.Contains("Local"))
267 fLbc.back().fPixmap = "hdisk_t.xpm";
268 else if (infos.Contains("CD"))
269 fLbc.back().fPixmap = "cdrom_t.xpm";
270 else if (infos.Contains("Network"))
271 fLbc.back().fPixmap = "netdisk_t.xpm";
272 }
273 delete volumes;
274 delete curvol;
275 }
276
277 Int_t cnt = 0;
278
279 for (auto &entry : fLbc) {
280 entry.fId = ++cnt * 1000; // assign ids after vector is created
281 if (entry.fPath.find("$HOME") == 0) {
282 if (homeDir) {
283 std::string newpath = homeDir;
284 newpath.append(entry.fPath.substr(5));
285 entry.fPath = newpath;
286 } else {
287 entry.fFlags = 0;
288 }
289 }
290#ifndef ROOTPREFIX
291 // Below should _only_ be called if the prefix isn't set at build
292 // time. The code below expands the occurance of `$ROOTSYS' in
293 // the table above. However, in the case of prefix being set at
294 // build time, we do not need to expand the prefix, as it is
295 // already known, so the entries in the table above are actually
296 // fully expanded.
297 if (entry.fPath.find("$ROOTSYS") == 0) {
298 // Get the size of the prefix template
299 const int plen = 8;
300 if (rootSys) {
301 std::string newpath = rootSys;
302 newpath.append(entry.fPath.substr(plen));
303 entry.fPath = newpath;
304 } else {
305 entry.fFlags = 0;
306 }
307 }
308#endif
309 if (gSystem->AccessPathName(entry.fPath.c_str(), kFileExists) == 0)
310 entry.fFlags = 1;
311 }
312
313 //--- then init the contents...
314
315 for (auto &entry : fLbc) {
316 if (entry.fFlags) {
317 int indent = 4 + (entry.fIndent * 10);
318 auto pic = fClient->GetPicture(entry.fPixmap.c_str());
319 if (!pic) Error("TGFSComboBox", "pixmap not found: %s", entry.fPixmap.c_str());
321 new TGString(entry.fName.c_str()), pic, entry.fId,
322 new TGString(entry.fPath.c_str())),
324 }
325 }
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// Update file system combo box.
331
332void TGFSComboBox::Update(const char *path)
333{
334 char dirname[1024], mpath[1024];
335 const char *tailpath = 0;
336 int indent_lvl = 0, afterID = -1, sel = -1;
337
338 if (!path) return;
339
340 for (int i = 0; i < (int) fLbc.size() - 1; ++i)
341 RemoveEntries(fLbc[i].fId+1, fLbc[i+1].fId-1);
342 RemoveEntries(fLbc.back().fId+1, (fLbc.size() + 1) * 1000 - 1);
343
344 int len = 0;
345 for (auto &entry : fLbc) {
346 if (entry.fFlags) {
347 int slen = entry.fPath.length();
348 if (strncmp(path, entry.fPath.c_str(), slen) == 0) {
349 if (slen > len) {
350 sel = afterID = entry.fId;
351 indent_lvl = entry.fIndent + 1;
352 if ((len > 0) && ((path[slen] == '\\') || (path[slen] == '/') ||
353 (path[slen] == 0)))
354 tailpath = path + slen;
355 strlcpy(mpath, entry.fPath.c_str(), 1024);
356 len = slen;
357 }
358 }
359 }
360 }
361
362 if (tailpath && *tailpath) {
363 if ((*tailpath == '/') || (*tailpath == '\\')) ++tailpath;
364 if (*tailpath)
365 while (1) {
366 const char *picname;
367 const char *semi = strchr(tailpath, '/');
368 if (semi == 0) semi = strchr(tailpath, '\\');
369 if (semi == 0) {
370 strlcpy(dirname, tailpath, 1024);
371 picname = "ofolder_t.xpm";
372 } else {
373 strlcpy(dirname, tailpath, (semi-tailpath)+1);
374 picname = "folder_t.xpm";
375 }
376 if ((mpath[strlen(mpath)-1] != '/') &&
377 (mpath[strlen(mpath)-1] != '\\')) {
378 strlcat(mpath, "/", 1024-strlen(mpath));
379 }
380 strlcat(mpath, dirname, 1024-strlen(mpath));
381 int indent = 4 + (indent_lvl * 10);
382 const TGPicture *pic = fClient->GetPicture(picname);
383 if (!pic) Error("Update", "pixmap not found: %s", picname);
385 new TGString(dirname), pic, afterID+1,
386 new TGString(mpath)),
388 indent, 0, 0, 0),
389 afterID);
390 sel = ++afterID;
391 ++indent_lvl;
392 if (semi == 0) break;
393 tailpath = ++semi;
394 }
395 }
396 if (sel > 0) Select(sel);
397}
398
399////////////////////////////////////////////////////////////////////////////////
400/// Save a file system combo box as a C++ statement(s) on output stream out.
401
402void TGFSComboBox::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
403{
404 if (fBackground != GetWhitePixel()) SaveUserColor(out, option);
405
406 out << std::endl << " // file system combo box" << std::endl;
407 out << " TGFSComboBox *";
408 out << GetName() << " = new TGFSComboBox(" << fParent->GetName()
409 << "," << fWidgetId;
410 if (fBackground == GetWhitePixel()) {
412 out <<");" << std::endl;
413 } else {
414 out << "," << GetOptionString() <<");" << std::endl;
415 }
416 } else {
417 out << "," << GetOptionString() << ",ucolor);" << std::endl;
418 }
419 if (option && strstr(option, "keep_names"))
420 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
421
422 out << " " << GetName() << "->Resize(" << GetWidth() << ","
423 << GetHeight() << ");" << std::endl;
424 out << " " << GetName() << "->Select(" << GetSelected() << ");" << std::endl;
425
426}
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
ULongptr_t Handle_t
Generic resource handle.
Definition GuiTypes.h:26
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
const Bool_t kFALSE
Definition RtypesCore.h:101
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:157
@ kLHintsExpandY
Definition TGLayout.h:31
@ kLHintsTop
Definition TGLayout.h:27
@ kLHintsExpandX
Definition TGLayout.h:30
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:124
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition TGClient.cxx:289
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition TGClient.cxx:372
A combobox (also known as a drop down listbox) allows the selection of one item out of a list of item...
Definition TGComboBox.h:47
virtual Int_t GetSelected() const
Definition TGComboBox.h:114
virtual void AddEntry(TGString *s, Int_t id)
Definition TGComboBox.h:86
TGListBox * fListBox
the listbox with text items
Definition TGComboBox.h:58
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:105
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:92
UInt_t fHeight
Definition TGDimension.h:21
UInt_t fWidth
Definition TGDimension.h:20
This is a combo box that is used in the File Selection dialog box.
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.
Encapsulate fonts used in the GUI system.
Definition TGFont.h:140
FontStruct_t GetFontStruct() const
Definition TGFont.h:184
void AddInput(UInt_t emask)
Add events specified in the emask to the events the frame should handle.
Definition TGFrame.cxx:339
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition TGFrame.cxx:709
UInt_t fHeight
frame height
Definition TGFrame.h:88
static Pixel_t fgDefaultSelectedBackground
Definition TGFrame.h:102
virtual UInt_t GetOptions() const
Definition TGFrame.h:197
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition TGFrame.cxx:2504
UInt_t fWidth
frame width
Definition TGFrame.h:87
UInt_t GetHeight() const
Definition TGFrame.h:225
static Pixel_t fgWhitePixel
Definition TGFrame.h:103
UInt_t GetWidth() const
Definition TGFrame.h:224
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition TGFrame.cxx:2477
Pixel_t fBackground
frame background color
Definition TGFrame.h:95
static Pixel_t fgBlackPixel
Definition TGFrame.h:104
Encapsulate a graphics context used in the low level graphics.
Definition TGGC.h:22
Basic listbox entries.
Definition TGListBox.h:24
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
virtual TGFrame * GetContainer() const
Definition TGListBox.h:267
TGClient * fClient
Connection to display server.
Definition TGObject.h:27
Handle_t fId
X11/Win32 Window identifier.
Definition TGObject.h:26
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
UInt_t GetHeight() const
Definition TGPicture.h:53
void Draw(Option_t *="")
Default Draw method for all objects.
Definition TGPicture.h:46
UInt_t GetWidth() const
Definition TGPicture.h:52
Pixel_t GetSelectedFgndColor() const
TGString wraps a TString and adds some graphics routines like drawing, size of string on screen depen...
Definition TGString.h:20
Int_t GetLength() const
Definition TGString.h:29
const char * GetString() const
Definition TGString.h:30
virtual void Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
Draw string.
Definition TGString.cxx:56
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.
UInt_t fTWidth
width of entry text
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
entry picture
Bool_t fActive
true if active
FontStruct_t fFontStruct
font
UInt_t fTHeight
height of entry text
TGString * fPath
entry path
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
entry drawing context
TGSelectedPicture * fSelPic
selected picture
TGString * fText
entry description
static FontStruct_t GetDefaultFontStruct()
Return default font structure.
Int_t fWidgetId
the widget id (used for event processing)
Definition TGWidget.h:46
ROOT GUI Window base class.
Definition TGWindow.h:23
virtual void SetWindowName(const char *name=0)
Set window name.
Definition TGWindow.cxx:129
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition TGWindow.cxx:336
const TGWindow * fParent
Parent window.
Definition TGWindow.h:28
A doubly linked list.
Definition TList.h:38
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:1663
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:1296
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:888
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:208