Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGInputDialog.cxx
Go to the documentation of this file.
1// @(#)root/gui:$Id$
2// Author: David Gonzalez Maline 19/07/2006
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/** \class TGInputDialog
14 \ingroup guiwidgets
15
16Input Dialog Widget
17
18An Input dialog box
19
20*/
21
22
23#include "TGInputDialog.h"
24#include "TGButton.h"
25#include "TGLabel.h"
26#include "TGTextEntry.h"
27
29
30
31////////////////////////////////////////////////////////////////////////////////
32/// Create simple input dialog.
33///
34/// It is important to know that the case where the constructor in
35/// which all the variables are initialized to their default values is
36/// only used for the TBrowser to inspect on the classes. For normal
37/// use the only variable that should be free is options.
38///
39/// Variables prompt, defval are the content of the input dialog while
40/// retstr has to be initialized to a char[256]. In case these are not
41/// initialized, they will show default values while retstr will be
42/// automatically allocated by the dialog. However this will make
43/// impossible to retrieve the value entered by the dialog.
44///
45/// To see TGInputDialog in use see:
46/// $ROOTSYS/tutorials/testInputDialog.cxx
47
49 const char *prompt, const char *defval,
50 char *retstr, UInt_t options) :
51 TGTransientFrame(p, main, 10, 10, options)
52{
53
54 if (!p && !main) {
55 MakeZombie();
56 // coverity [uninit_ctor]
57 return;
58 }
60 // create prompt label and textentry widget
61 fLabel = new TGLabel(this, prompt?prompt:"Introduce value:");
62
63 TGTextBuffer *tbuf = new TGTextBuffer(256); //will be deleted by TGtextEntry
64 tbuf->AddText(0, defval?defval:"");
65
66 fTE = new TGTextEntry(this, tbuf);
68
70 AddFrame(fTE, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5));
71
72 // create frame and layout hints for Ok and Cancel buttons
73 TGHorizontalFrame *hf = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
75
76 // create OK and Cancel buttons in their own frame (hf)
77 UInt_t width = 0, height = 0;
78
79 fOk = new TGTextButton(hf, "&Ok", 1);
80 fOk->Associate(this);
83
84 fCancel = new TGTextButton(hf, "&Cancel", 2);
85 fCancel->Associate(this);
89
90 // place button frame (hf) at the bottom
91 AddFrame(hf, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
92
93 // keep buttons centered and with the same width
94 hf->Resize((width + 20) * 2, height);
95
96 // set dialog title
97 SetWindowName("Get Input");
98
99 // map all widgets and calculate size of dialog
101
104
106
107 // position relative to the parent's window
109
110 // make the message box non-resizable
113
118
119 // popup dialog and wait till user replies
120 MapWindow();
121 fTE->SetFocus();
122
123 if (!retstr)
124 retstr = fOwnBuf = new char[256];
125
126 fRetStr = retstr;
127
128 gClient->WaitFor(this);
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Cleanup dialog.
133
135{
136 Cleanup();
137 delete [] fOwnBuf;
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Handle button and text enter events
142
144{
145 switch (GET_MSG(msg)) {
146 case kC_COMMAND:
147 switch (GET_SUBMSG(msg)) {
148 case kCM_BUTTON:
149 switch (parm1) {
150 case 1:
151 // here copy the string from text buffer to return variable
152 // coverity[secure_coding]
153 strcpy(fRetStr, fTE->GetBuffer()->GetString()); // NOLINT
154 // if user selected an empty string, set the second
155 // char to 1,in order to distinguish between empty string
156 // selected with OK and Cancel button pressed
157 if (!strcmp(fRetStr, ""))
158 fRetStr[1] = 1;
159 delete this;
160 break;
161 case 2:
162 // hack to detect the case where the user pressed the
163 // Cancel button
164 fRetStr[0] = 0;
165 fRetStr[1] = 0;
166 delete this;
167 break;
168 }
169 default:
170 break;
171 }
172 break;
173
174 case kC_TEXTENTRY:
175 switch (GET_SUBMSG(msg)) {
176 case kTE_ENTER:
177 // here copy the string from text buffer to return variable
178 // coverity[secure_coding]
179 strcpy(fRetStr, fTE->GetBuffer()->GetString()); // NOLINT
180 // if user selected an empty string, set the second
181 // char to 1,in order to distinguish between empty string
182 // selected with OK and Cancel button pressed
183 if (!strcmp(fRetStr, ""))
184 fRetStr[1] = 1;
185 delete this;
186 break;
187 default:
188 break;
189 }
190 break;
191
192 default:
193 break;
194 }
195 return kTRUE;
196}
@ kFixedWidth
Definition GuiTypes.h:387
int main()
Definition Prototype.cxx:12
long Longptr_t
Definition RtypesCore.h:75
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassImp(name)
Definition Rtypes.h:382
#define gClient
Definition TGClient.h:156
@ kMWMDecorResizeH
Definition TGFrame.h:65
@ kMWMFuncAll
Definition TGFrame.h:49
@ kMWMFuncResize
Definition TGFrame.h:50
@ kMWMDecorMaximize
Definition TGFrame.h:69
@ kMWMDecorMinimize
Definition TGFrame.h:68
@ kMWMDecorMenu
Definition TGFrame.h:67
@ kMWMDecorAll
Definition TGFrame.h:63
@ kMWMFuncMaximize
Definition TGFrame.h:53
@ kMWMInputModeless
Definition TGFrame.h:57
@ kMWMFuncMinimize
Definition TGFrame.h:52
@ kDeepCleanup
Definition TGFrame.h:42
@ kLHintsLeft
Definition TGLayout.h:24
@ kLHintsCenterY
Definition TGLayout.h:28
@ kLHintsCenterX
Definition TGLayout.h:25
@ kLHintsBottom
Definition TGLayout.h:29
@ kLHintsTop
Definition TGLayout.h:27
@ kLHintsExpandX
Definition TGLayout.h:30
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void SetMWMHints
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t SetWMSizeHints
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
Int_t GET_MSG(Long_t val)
@ kTE_ENTER
@ kC_COMMAND
@ kCM_BUTTON
@ kC_TEXTENTRY
Int_t GET_SUBMSG(Long_t val)
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
UInt_t GetDefaultWidth() const override
Definition TGFrame.h:312
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition TGFrame.cxx:967
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1164
UInt_t GetDefaultHeight() const override
Definition TGFrame.h:314
void SetCleanup(Int_t mode=kLocalCleanup) override
Turn on automatic cleanup of child frames in dtor.
Definition TGFrame.cxx:1072
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:605
virtual UInt_t GetDefaultWidth() const
Definition TGFrame.h:190
virtual UInt_t GetDefaultHeight() const
Definition TGFrame.h:191
void MapWindow() override
map window
Definition TGFrame.h:204
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:385
Input Dialog Widget.
TGInputDialog(const TGInputDialog &)=delete
TGTextButton * fCancel
cancel button
char * fOwnBuf
internal buffer when return string not specified
char * fRetStr
address to store return string
Bool_t ProcessMessage(Longptr_t msg, Longptr_t parm1, Longptr_t) override
Handle button and text enter events.
TGTextButton * fOk
ok button
~TGInputDialog() override
Cleanup dialog.
TGTextEntry * fTE
text entry widget
TGLabel * fLabel
text entry label
This class handles GUI labels.
Definition TGLabel.h:24
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
void SetWMSize(UInt_t w, UInt_t h)
Give the window manager a window size hint.
Definition TGFrame.cxx:1893
void SetWindowName(const char *name=nullptr) override
Set window name. This is typically done via the window manager.
Definition TGFrame.cxx:1788
A text buffer is used in several widgets, like TGTextEntry, TGFileDialog, etc.
const char * GetString() const
Yield an action as soon as it is clicked.
Definition TGButton.h:142
A TGTextEntry is a one line text input widget.
Definition TGTextEntry.h:24
virtual void SetFocus()
Set focus to this text entry.
TGTextBuffer * GetBuffer() const
Defines transient windows that typically are used for dialogs windows.
Definition TGFrame.h:498
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition TGFrame.cxx:1957
virtual void Associate(const TGWindow *w)
Definition TGWidget.h:72
ROOT GUI Window base class.
Definition TGWindow.h:23
void MakeZombie()
Definition TObject.h:53
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250