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// //
14// Input Dialog Widget //
15// //
16// An Input dialog box //
17// //
18//////////////////////////////////////////////////////////////////////////
19
20#include "TGInputDialog.h"
21#include "TGButton.h"
22#include "TGLabel.h"
23#include "TGTextEntry.h"
24
26
27
28////////////////////////////////////////////////////////////////////////////////
29
31 const char *prompt, const char *defval,
32 char *retstr, UInt_t options) :
33 TGTransientFrame(p, main, 10, 10, options)
34{
35 /** Create simple input dialog.
36
37 It is important to know that the case where the constructor in
38 which all the variables are initialized to their default values is
39 only used for the TBrowser to inspect on the classes. For normal
40 use the only variable that should be free is options.
41
42 Variables prompt, defval are the content of the input dialog while
43 retstr has to be initialized to a char[256]. In case these are not
44 initialized, they will show default values while retstr will be
45 automatically allocated by the dialog. However this will make
46 impossible to retrieve the value entered by the dialog.
47
48 To see TGInputDialog in use see:
49 $ROOTSYS/tutorials/testInputDialog.cxx
50 */
51
52 if (!p && !main) {
53 MakeZombie();
54 // coverity [uninit_ctor]
55 return;
56 }
58 // create prompt label and textentry widget
59 fLabel = new TGLabel(this, prompt?prompt:"Introduce value:");
60
61 TGTextBuffer *tbuf = new TGTextBuffer(256); //will be deleted by TGtextEntry
62 tbuf->AddText(0, defval?defval:"");
63
64 fTE = new TGTextEntry(this, tbuf);
66
68 AddFrame(fTE, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5));
69
70 // create frame and layout hints for Ok and Cancel buttons
71 TGHorizontalFrame *hf = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
73
74 // create OK and Cancel buttons in their own frame (hf)
75 UInt_t width = 0, height = 0;
76
77 fOk = new TGTextButton(hf, "&Ok", 1);
78 fOk->Associate(this);
80 height = fOk->GetDefaultHeight();
82
83 fCancel = new TGTextButton(hf, "&Cancel", 2);
84 fCancel->Associate(this);
86 height = fCancel->GetDefaultHeight();
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
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());
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());
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:387
long Long_t
Definition: RtypesCore.h:52
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
#define gClient
Definition: TGClient.h:166
@ kMWMDecorResizeH
Definition: TGFrame.h:74
@ kMWMFuncAll
Definition: TGFrame.h:58
@ kMWMFuncResize
Definition: TGFrame.h:59
@ kMWMDecorMaximize
Definition: TGFrame.h:78
@ kMWMDecorMinimize
Definition: TGFrame.h:77
@ kMWMDecorMenu
Definition: TGFrame.h:76
@ kMWMDecorAll
Definition: TGFrame.h:72
@ kMWMFuncMaximize
Definition: TGFrame.h:62
@ kMWMInputModeless
Definition: TGFrame.h:66
@ kMWMFuncMinimize
Definition: TGFrame.h:61
@ kDeepCleanup
Definition: TGFrame.h:51
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsCenterY
Definition: TGLayout.h:35
@ kLHintsCenterX
Definition: TGLayout.h:32
@ kLHintsBottom
Definition: TGLayout.h:36
@ kLHintsTop
Definition: TGLayout.h:34
@ kLHintsExpandX
Definition: TGLayout.h:37
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=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1101
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:349
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:951
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1056
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1148
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:351
virtual UInt_t GetDefaultWidth() const
Definition: TGFrame.h:215
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:216
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:589
virtual void MapWindow()
map window
Definition: TGFrame.h:229
TGInputDialog(const TGInputDialog &)=delete
TGTextButton * fCancel
Definition: TGInputDialog.h:35
~TGInputDialog()
Cleanup dialog.
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t)
Handle button and text enter events.
TGTextButton * fOk
Definition: TGInputDialog.h:34
TGTextEntry * fTE
Definition: TGInputDialog.h:33
TGLabel * fLabel
Definition: TGInputDialog.h:32
void SetWMSize(UInt_t w, UInt_t h)
Give the window manager a window size hint.
Definition: TGFrame.cxx:1851
void SetMWMHints(UInt_t value, UInt_t funcs, UInt_t input)
Set decoration style for MWM-compatible wm (mwm, ncdwm, fvwm?).
Definition: TGFrame.cxx:1826
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:1864
void SetWindowName(const char *name=0)
Set window name. This is typically done via the window manager.
Definition: TGFrame.cxx:1748
void AddText(Int_t pos, const char *text)
Definition: TGTextBuffer.h:49
const char * GetString() const
Definition: TGTextBuffer.h:47
virtual void SetFocus()
Set focus to this text entry.
TGTextBuffer * GetBuffer() const
Definition: TGTextEntry.h:127
virtual void CenterOnParent(Bool_t croot=kTRUE, EPlacement pos=kCenter)
Position transient frame centered relative to the parent frame.
Definition: TGFrame.cxx:1915
virtual void Associate(const TGWindow *w)
Definition: TGWidget.h:84
void MakeZombie()
Definition: TObject.h:49
int main(int argc, char **argv)
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212