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
50
51////////////////////////////////////////////////////////////////////////////////
52/// Create a tree (i.e. entry can be indented) listbox entry.
53/// The strings text and path are adopted by the entry.
54
56 const TGPicture *pic, Int_t id, TGString *path,
57 GContext_t norm, FontStruct_t font, UInt_t options,
58 ULong_t back) :
59 TGLBEntry(p, id, options, back)
60{
61 if (text && !pic)
62 Error("TGTreeLBEntry", "icon not found for entry %s", text->GetString());
63
64 fPic = pic;
65 fSelPic = 0;
66 fTWidth = 0;
67 fText = text;
68 fPath = path;
69
70 fNormGC = norm;
71 fFontStruct = font;
72
74
76 if (fText)
78 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Delete tree listbox entry.
85
87{
88 delete fText;
89 delete fPath;
90 delete fSelPic;
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Make entry active (highlight picture).
95
97{
98 if (fActive == a) return;
99 fActive = a;
100
101 if (fActive) {
103 } else {
104 if (fSelPic) delete fSelPic;
105 fSelPic = 0;
106 }
107 DoRedraw();
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// Redraw the tree listbox entry on pixmap/window.
112
114{
115 int ix, iy, lx, ly;
116
117 ix = x;
118 iy = y + ((fHeight - fPic->GetHeight()) >> 1);
119 lx = x + (int)(fPic->GetWidth() + 4);
120 ly = y + (int)((fHeight - (fTHeight+1)) >> 1);
121
122 if (fActive) {
123 if (fSelPic) fSelPic->Draw(id, fNormGC, ix, iy);
125 gVirtualX->FillRectangle(id, fNormGC, lx-2, ly, fWidth-(lx-x), fTHeight+1);
126 gVirtualX->SetForeground(fNormGC, fClient->GetResourcePool()->GetSelectedFgndColor());
127 } else {
128 fPic->Draw(id, fNormGC, ix, iy);
129 gVirtualX->SetForeground(fNormGC, fgWhitePixel);
130 gVirtualX->FillRectangle(id, fNormGC, lx-2, ly, fWidth-(lx-x), fTHeight+1);
131 gVirtualX->SetForeground(fNormGC, fgBlackPixel);
132 }
133
135 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
136
137 fText->Draw(id, fNormGC, lx, ly + max_ascent);
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Redraw the tree listbox entry.
142
144{
145 DrawCopy(fId, 0, 0);
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Return default size of tree listbox entry.
150
152{
153 TGDimension isize(fPic->GetWidth(), fPic->GetHeight());
155
156 return TGDimension(isize.fWidth + lsize.fWidth + 4,
157 std::max(isize.fHeight, lsize.fHeight) + 2);
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Update text and picture of a listbox entry.
162
164{
166
167 if (fText) delete fText;
168 fText = new TGString(te->GetText());
169 fPic = te->GetPicture();
171 gVirtualX->ClearWindow(fId);
172 fClient->NeedRedraw(this);
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Return default font structure.
177
179{
180 if (!fgDefaultFont)
181 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
182 return fgDefaultFont->GetFontStruct();
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Return default graphics context.
187
189{
190 if (!fgDefaultGC)
191 fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
192 return *fgDefaultGC;
193}
194
195
196////////////////////////////////////////////////////////////////////////////////
197/// Create a file system combobox showing system directories.
198
200 ULong_t back) :
201 TGComboBox(parent, id, options | kOwnBackground, back)
202{
203 SetTopEntry(new TGTreeLBEntry(this, new TGString("Current dir"),
204 fClient->GetPicture("folder_t.xpm"), 0),
205 new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 4, 0, 0, 0));
206
209
210 //--- first check for the existence of some directories...
211
212 const char *homeDir = gSystem->HomeDirectory();
213#ifndef ROOTPREFIX
214 const char *rootSys = gSystem->Getenv("ROOTSYS");
215#else
216 // const char *rootSys = ROOTPREFIX;
217#endif
218
219 TList *volumes = gSystem->GetVolumes("all");
220 TList *curvol = gSystem->GetVolumes("cur");
221 const char *curdrive = "";
222 if (volumes && curvol) {
223 TNamed *named = (TNamed *)curvol->At(0);
224 if (named) {
225 curdrive = named->GetName();
226 TString infos = named->GetTitle();
227
228 fLbc.emplace_back(infos.Data(), Form("%s\\", curdrive), "hdisk_t.xpm", 0);
229 if (infos.Contains("Removable"))
230 fLbc.back().fPixmap = "fdisk_t.xpm";
231 else if (infos.Contains("Local"))
232 fLbc.back().fPixmap = "hdisk_t.xpm";
233 else if (infos.Contains("CD"))
234 fLbc.back().fPixmap = "cdrom_t.xpm";
235 else if (infos.Contains("Network"))
236 fLbc.back().fPixmap = "netdisk_t.xpm";
237 } else {
238 fLbc.emplace_back("Root", "/", "hdisk_t.xpm", 1);
239 }
240 } else {
241 fLbc.emplace_back("Root", "/", "hdisk_t.xpm", 1);
242 fLbc.emplace_back("Floppy", "/floppy", "fdisk_t.xpm", 1);
243 fLbc.emplace_back("CD-ROM", "/cdrom", "cdrom_t.xpm", 1);
244 }
245 fLbc.emplace_back("Home", "$HOME", "home_t.xpm", 1);
246#ifndef ROOTPREFIX
247 fLbc.emplace_back("RootSys", "$ROOTSYS", "root_t.xpm", 1);
248#else
249 fLbc.emplace_back(ROOTPREFIX, ROOTPREFIX, "root_t.xpm", 1);
250#endif
251
252 if (volumes && curvol) {
253 TIter next(volumes);
254 TNamed *drive;
255 while ((drive = (TNamed *)next())) {
256 if (!strcmp(drive->GetName(), curdrive))
257 continue;
258 TString infos = drive->GetTitle();
259
260 fLbc.emplace_back(drive->GetTitle(), Form("%s\\", drive->GetName()), "hdisk_t.xpm", 0);
261
262 if (infos.Contains("Removable"))
263 fLbc.back().fPixmap = "fdisk_t.xpm";
264 else if (infos.Contains("Local"))
265 fLbc.back().fPixmap = "hdisk_t.xpm";
266 else if (infos.Contains("CD"))
267 fLbc.back().fPixmap = "cdrom_t.xpm";
268 else if (infos.Contains("Network"))
269 fLbc.back().fPixmap = "netdisk_t.xpm";
270 }
271 delete volumes;
272 delete curvol;
273 }
274
275 Int_t cnt = 0;
276
277 for (auto &entry : fLbc) {
278 entry.fId = ++cnt * 1000; // assign ids after vector is created
279 if (entry.fPath.find("$HOME") == 0) {
280 if (homeDir) {
281 std::string newpath = homeDir;
282 newpath.append(entry.fPath.substr(5));
283 entry.fPath = newpath;
284 } else {
285 entry.fFlags = 0;
286 }
287 }
288#ifndef ROOTPREFIX
289 // Below should _only_ be called if the prefix isn't set at build
290 // time. The code below expands the occurance of `$ROOTSYS' in
291 // the table above. However, in the case of prefix being set at
292 // build time, we do not need to expand the prefix, as it is
293 // already known, so the entries in the table above are actually
294 // fully expanded.
295 if (entry.fPath.find("$ROOTSYS") == 0) {
296 // Get the size of the prefix template
297 const int plen = 8;
298 if (rootSys) {
299 std::string newpath = rootSys;
300 newpath.append(entry.fPath.substr(plen));
301 entry.fPath = newpath;
302 } else {
303 entry.fFlags = 0;
304 }
305 }
306#endif
307 if (gSystem->AccessPathName(entry.fPath.c_str(), kFileExists) == 0)
308 entry.fFlags = 1;
309 }
310
311 //--- then init the contents...
312
313 for (auto &entry : fLbc) {
314 if (entry.fFlags) {
315 int indent = 4 + (entry.fIndent * 10);
316 auto pic = fClient->GetPicture(entry.fPixmap.c_str());
317 if (!pic) Error("TGFSComboBox", "pixmap not found: %s", entry.fPixmap.c_str());
319 new TGString(entry.fName.c_str()), pic, entry.fId,
320 new TGString(entry.fPath.c_str())),
322 }
323 }
325}
326
327////////////////////////////////////////////////////////////////////////////////
328/// Update file system combo box.
329
330void TGFSComboBox::Update(const char *path)
331{
332 char dirname[1024], mpath[1024];
333 const char *tailpath = 0;
334 int indent_lvl = 0, afterID = -1, sel = -1;
335
336 if (!path) return;
337
338 for (int i = 0; i < (int) fLbc.size() - 1; ++i)
339 RemoveEntries(fLbc[i].fId+1, fLbc[i+1].fId-1);
340 RemoveEntries(fLbc.back().fId+1, (fLbc.size() + 1) * 1000 - 1);
341
342 int len = 0;
343 for (auto &entry : fLbc) {
344 if (entry.fFlags) {
345 int slen = entry.fPath.length();
346 if (strncmp(path, entry.fPath.c_str(), slen) == 0) {
347 if (slen > len) {
348 sel = afterID = entry.fId;
349 indent_lvl = entry.fIndent + 1;
350 if ((len > 0) && ((path[slen] == '\\') || (path[slen] == '/') ||
351 (path[slen] == 0)))
352 tailpath = path + slen;
353 strlcpy(mpath, entry.fPath.c_str(), 1024);
354 len = slen;
355 }
356 }
357 }
358 }
359
360 if (tailpath && *tailpath) {
361 if ((*tailpath == '/') || (*tailpath == '\\')) ++tailpath;
362 if (*tailpath)
363 while (1) {
364 const char *picname;
365 const char *semi = strchr(tailpath, '/');
366 if (semi == 0) semi = strchr(tailpath, '\\');
367 if (semi == 0) {
368 strlcpy(dirname, tailpath, 1024);
369 picname = "ofolder_t.xpm";
370 } else {
372 picname = "folder_t.xpm";
373 }
374 if ((mpath[strlen(mpath)-1] != '/') &&
375 (mpath[strlen(mpath)-1] != '\\')) {
376 strlcat(mpath, "/", 1024-strlen(mpath));
377 }
379 int indent = 4 + (indent_lvl * 10);
381 if (!pic) Error("Update", "pixmap not found: %s", picname);
383 new TGString(dirname), pic, afterID+1,
384 new TGString(mpath)),
386 indent, 0, 0, 0),
387 afterID);
388 sel = ++afterID;
389 ++indent_lvl;
390 if (semi == 0) break;
391 tailpath = ++semi;
392 }
393 }
394 if (sel > 0) Select(sel);
395}
396
397////////////////////////////////////////////////////////////////////////////////
398/// Save a file system combo box as a C++ statement(s) on output stream out.
399
400void TGFSComboBox::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
401{
402 // store options and color if differ from defauls
404
405 out << "\n // file system combo box\n";
406 out << " TGFSComboBox *" << GetName() << " = new TGFSComboBox(" << fParent->GetName()
407 << "," << fWidgetId << extra_args << ");\n";
408 if (option && strstr(option, "keep_names"))
409 out << " " << GetName() << "->SetName(\"" << GetName() << "\");\n";
410
411 out << " " << GetName() << "->Resize(" << GetWidth() << ","
412 << GetHeight() << ");\n";
413 out << " " << GetName() << "->Select(" << GetSelected() << ");\n";
414
415}
const Mask_t kButtonPressMask
Definition GuiTypes.h:161
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:38
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
const Mask_t kButtonReleaseMask
Definition GuiTypes.h:162
ULongptr_t Handle_t
Generic resource handle.
Definition GuiTypes.h:26
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
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
#define gClient
Definition TGClient.h:157
@ kLHintsExpandY
Definition TGLayout.h:31
@ kLHintsTop
Definition TGLayout.h:27
@ kLHintsExpandX
Definition TGLayout.h:30
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t sel
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char text
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2495
@ kFileExists
Definition TSystem.h:52
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
#define gVirtualX
Definition TVirtualX.h:337
const TGResourcePool * GetResourcePool() const
Definition TGClient.h:124
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:380
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
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save a file system combo box as a C++ statement(s) on output stream out.
TGFSComboBox(const TGWindow *p=nullptr, 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
UInt_t fHeight
frame height
Definition TGFrame.h:88
static Pixel_t fgDefaultSelectedBackground
Definition TGFrame.h:102
UInt_t fWidth
frame width
Definition TGFrame.h:87
UInt_t GetHeight() const
Definition TGFrame.h:227
TString SaveCtorArgs(std::ostream &out, UInt_t dflt_options=kChildFrame, Bool_t check_white_pixel=kFALSE)
Return options and custom color as constructor args Used in the SavePrimitive methods,...
Definition TGFrame.cxx:2493
static Pixel_t fgWhitePixel
Definition TGFrame.h:103
UInt_t GetWidth() const
Definition TGFrame.h:226
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:25
Handle_t fId
X11/Win32 Window identifier.
Definition TGObject.h:24
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
void Draw(Option_t *="") override
Default Draw method for all objects.
Definition TGPicture.h:46
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:54
static const TGFont * fgDefaultFont
static TGGC * fgDefaultGC
UInt_t fTWidth
width of entry text
void Activate(Bool_t a) override
Make entry active (highlight picture).
const TGPicture * fPic
entry picture
Bool_t fActive
true if active
TGDimension GetDefaultSize() const override
Return default size of tree listbox entry.
FontStruct_t fFontStruct
font
UInt_t fTHeight
height of entry text
TGString * fPath
entry path
static const TGGC & GetDefaultGC()
Return default graphics context.
GContext_t fNormGC
entry drawing context
~TGTreeLBEntry() override
Delete tree listbox entry.
TGSelectedPicture * fSelPic
selected picture
TGString * fText
entry description
void Update(TGLBEntry *e) override
Update text and picture of a listbox entry.
TGTreeLBEntry(const TGWindow *p=nullptr, TGString *text=nullptr, const TGPicture *pic=nullptr, Int_t id=-1, TGString *path=nullptr, GContext_t norm=GetDefaultGC()(), FontStruct_t font=GetDefaultFontStruct(), UInt_t options=kHorizontalFrame, Pixel_t back=GetWhitePixel())
Create a tree (i.e.
static FontStruct_t GetDefaultFontStruct()
Return default font structure.
void DrawCopy(Handle_t id, Int_t x, Int_t y) override
Redraw the tree listbox entry on pixmap/window.
void DoRedraw() override
Redraw the tree listbox entry.
Int_t fWidgetId
the widget id (used for event processing)
Definition TGWidget.h:46
ROOT GUI Window base class.
Definition TGWindow.h:23
const TGWindow * fParent
Parent window.
Definition TGWindow.h:28
virtual void SetWindowName(const char *name=nullptr)
Set window name.
Definition TGWindow.cxx:127
const char * GetName() const override
Return unique name, used in SavePrimitive methods.
Definition TGWindow.cxx:334
A doubly linked list.
Definition TList.h:38
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
Basic string class.
Definition TString.h:138
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1676
virtual TList * GetVolumes(Option_t *) const
Definition TSystem.h:465
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:1307
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:897
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17