Logo ROOT  
Reference Guide
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);
87 height = fCancel->GetDefaultHeight();
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
103 height = GetDefaultHeight();
104
105 Resize(width, height);
106
107 // position relative to the parent's window
109
110 // make the message box non-resizable
111 SetWMSize(width, height);
112 SetWMSizeHints(width, height, width, height, 0, 0);
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
long Longptr_t
Definition: RtypesCore.h:82
unsigned int UInt_t
Definition: RtypesCore.h:46
bool Bool_t
Definition: RtypesCore.h:63
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:364
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
#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)
int main(int argc, char *argv[])
Definition: cef_main.cxx:54
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1117
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:312
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:967
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1072
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1164
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:314
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:190
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:191
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:605
virtual void MapWindow()
map window
Definition: TGFrame.h:204
A composite frame that layout their children in horizontal way.
Definition: TGFrame.h:386
Input Dialog Widget.
Definition: TGInputDialog.h:22
TGInputDialog(const TGInputDialog &)=delete
TGTextButton * fCancel
cancel button
Definition: TGInputDialog.h:28
char * fOwnBuf
internal buffer when return string not specified
Definition: TGInputDialog.h:30
char * fRetStr
address to store return string
Definition: TGInputDialog.h:29
virtual Bool_t ProcessMessage(Longptr_t msg, Longptr_t parm1, Longptr_t)
Handle button and text enter events.
~TGInputDialog()
Cleanup dialog.
TGTextButton * fOk
ok button
Definition: TGInputDialog.h:27
TGTextEntry * fTE
text entry widget
Definition: TGInputDialog.h:26
TGLabel * fLabel
text entry label
Definition: TGInputDialog.h:25
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:1891
void SetMWMHints(UInt_t value, UInt_t funcs, UInt_t input)
Set decoration style for MWM-compatible wm (mwm, ncdwm, fvwm?).
Definition: TGFrame.cxx:1866
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:1904
void SetWindowName(const char *name=0)
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.
Definition: TGTextBuffer.h:19
void AddText(Int_t pos, const char *text)
Definition: TGTextBuffer.h:36
const char * GetString() const
Definition: TGTextBuffer.h:34
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
Definition: TGTextEntry.h:112
Defines transient windows that typically are used for dialogs windows.
Definition: TGFrame.h:499
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition: TGFrame.cxx:1955
virtual void Associate(const TGWindow *w)
Definition: TGWidget.h:72
ROOT GUI Window base class.
Definition: TGWindow.h:23
void MakeZombie()
Definition: TObject.h:49
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:208