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
28
29
30////////////////////////////////////////////////////////////////////////////////
31/// Create simple input dialog.
32///
33/// It is important to know that the case where the constructor in
34/// which all the variables are initialized to their default values is
35/// only used for the TBrowser to inspect on the classes. For normal
36/// use the only variable that should be free is options.
37///
38/// Variables prompt, defval are the content of the input dialog while
39/// retstr has to be initialized to a char[256]. In case these are not
40/// initialized, they will show default values while retstr will be
41/// automatically allocated by the dialog. However this will make
42/// impossible to retrieve the value entered by the dialog.
43///
44/// To see TGInputDialog in use see:
45/// $ROOTSYS/tutorials/testInputDialog.cxx
46
48 const char *prompt, const char *defval,
49 char *retstr, UInt_t options) :
50 TGTransientFrame(p, main, 10, 10, options)
51{
52
53 if (!p && !main) {
54 MakeZombie();
55 // coverity [uninit_ctor]
56 return;
57 }
59 // create prompt label and textentry widget
60 fLabel = new TGLabel(this, prompt?prompt:"Introduce value:");
61
62 TGTextBuffer *tbuf = new TGTextBuffer(256); //will be deleted by TGtextEntry
63 tbuf->AddText(0, defval?defval:"");
64
65 fTE = new TGTextEntry(this, tbuf);
66 fTE->Resize(260, fTE->GetDefaultHeight());
67
69 AddFrame(fTE, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5));
70
71 // create frame and layout hints for Ok and Cancel buttons
72 TGHorizontalFrame *hf = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
74
75 // create OK and Cancel buttons in their own frame (hf)
76 UInt_t width = 0, height = 0;
77
78 fOk = new TGTextButton(hf, "&Ok", 1);
79 fOk->Associate(this);
81 width = std::max(width, fOk->GetDefaultWidth());
82
83 fCancel = new TGTextButton(hf, "&Cancel", 2);
84 fCancel->Associate(this);
86 height = fCancel->GetDefaultHeight();
87 width = std::max(width, fCancel->GetDefaultWidth());
88
89 // place button frame (hf) at the bottom
90 AddFrame(hf, new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5));
91
92 // keep buttons centered and with the same width
93 hf->Resize((width + 20) * 2, height);
94
95 // set dialog title
96 SetWindowName("Get Input");
97
98 // map all widgets and calculate size of dialog
100
101 width = GetDefaultWidth();
102 height = GetDefaultHeight();
103
104 Resize(width, height);
105
106 // position relative to the parent's window
108
109 // make the message box non-resizable
110 SetWMSize(width, height);
111 SetWMSizeHints(width, height, width, height, 0, 0);
112
117
118 // popup dialog and wait till user replies
119 MapWindow();
120 fTE->SetFocus();
121
122 if (!retstr)
123 retstr = fOwnBuf = new char[256];
124
125 fRetStr = retstr;
126
127 gClient->WaitFor(this);
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Cleanup dialog.
132
134{
135 Cleanup();
136 delete [] fOwnBuf;
137}
138
139////////////////////////////////////////////////////////////////////////////////
140/// Handle button and text enter events
141
143{
144 switch (GET_MSG(msg)) {
145 case kC_COMMAND:
146 switch (GET_SUBMSG(msg)) {
147 case kCM_BUTTON:
148 switch (parm1) {
149 case 1:
150 // here copy the string from text buffer to return variable
151 // coverity[secure_coding]
152 strcpy(fRetStr, fTE->GetBuffer()->GetString()); // NOLINT
153 // if user selected an empty string, set the second
154 // char to 1,in order to distinguish between empty string
155 // selected with OK and Cancel button pressed
156 if (!strcmp(fRetStr, ""))
157 fRetStr[1] = 1;
158 delete this;
159 break;
160 case 2:
161 // hack to detect the case where the user pressed the
162 // Cancel button
163 fRetStr[0] = 0;
164 fRetStr[1] = 0;
165 delete this;
166 break;
167 }
168 default:
169 break;
170 }
171 break;
172
173 case kC_TEXTENTRY:
174 switch (GET_SUBMSG(msg)) {
175 case kTE_ENTER:
176 // here copy the string from text buffer to return variable
177 // coverity[secure_coding]
178 strcpy(fRetStr, fTE->GetBuffer()->GetString()); // NOLINT
179 // if user selected an empty string, set the second
180 // char to 1,in order to distinguish between empty string
181 // selected with OK and Cancel button pressed
182 if (!strcmp(fRetStr, ""))
183 fRetStr[1] = 1;
184 delete this;
185 break;
186 default:
187 break;
188 }
189 break;
190
191 default:
192 break;
193 }
194 return kTRUE;
195}
@ kFixedWidth
Definition GuiTypes.h:388
long Longptr_t
Integer large enough to hold a pointer (platform-dependent).
Definition RtypesCore.h:89
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
#define gClient
Definition TGClient.h:157
@ 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
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:1109
UInt_t GetDefaultWidth() const override
Definition TGFrame.h:314
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition TGFrame.cxx:959
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1156
UInt_t GetDefaultHeight() const override
Definition TGFrame.h:316
void SetCleanup(Int_t mode=kLocalCleanup) override
Turn on automatic cleanup of child frames in dtor.
Definition TGFrame.cxx:1064
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:597
void MapWindow() override
map window
Definition TGFrame.h:206
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:387
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:1885
void SetWindowName(const char *name=nullptr) override
Set window name. This is typically done via the window manager.
Definition TGFrame.cxx:1780
void SetMWMHints(UInt_t value, UInt_t funcs, UInt_t input)
Set decoration style for MWM-compatible wm (mwm, ncdwm, fvwm?).
Definition TGFrame.cxx:1860
void SetWMSizeHints(UInt_t wmin, UInt_t hmin, UInt_t wmax, UInt_t hmax, UInt_t winc, UInt_t hinc)
Give the window manager minimum and maximum size hints.
Definition TGFrame.cxx:1898
A text buffer is used in several widgets, like TGTextEntry, TGFileDialog, etc.
void AddText(Int_t pos, const char *text)
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
TGTransientFrame(const TGTransientFrame &)=delete
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition TGFrame.cxx:1949
ROOT GUI Window base class.
Definition TGWindow.h:23
void MakeZombie()
Definition TObject.h:55
int main(int argc, char **argv)
Definition hadd.cxx:631