Logo ROOT   6.10/09
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  }
57  SetCleanup(kDeepCleanup);
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);
65  fTE->Resize(260, fTE->GetDefaultHeight());
66 
67  AddFrame(fLabel, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
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);
79  hf->AddFrame(fOk, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0));
80  height = fOk->GetDefaultHeight();
81  width = TMath::Max(width, fOk->GetDefaultWidth());
82 
83  fCancel = new TGTextButton(hf, "&Cancel", 2);
84  fCancel->Associate(this);
85  hf->AddFrame(fCancel, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0));
86  height = fCancel->GetDefaultHeight();
87  width = TMath::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
99  MapSubwindows();
100 
101  width = GetDefaultWidth();
102  height = GetDefaultHeight();
103 
104  Resize(width, height);
105 
106  // position relative to the parent's window
107  CenterOnParent();
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 == 0)
123  retstr = new char[256];
124 
125  fRetStr = retstr;
126 
127  gClient->WaitFor(this);
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Cleanup dialog.
132 
134 {
135  Cleanup();
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Handle button and text enter events
140 
142 {
143  switch (GET_MSG(msg)) {
144  case kC_COMMAND:
145  switch (GET_SUBMSG(msg)) {
146  case kCM_BUTTON:
147  switch (parm1) {
148  case 1:
149  // here copy the string from text buffer to return variable
150  // coverity[secure_coding]
151  strcpy(fRetStr, fTE->GetBuffer()->GetString());
152  // if user selected an empty string, set the second
153  // char to 1,in order to distinguish between empty string
154  // selected with OK and Cancel button pressed
155  if (!strcmp(fRetStr, ""))
156  fRetStr[1] = 1;
157  delete this;
158  break;
159  case 2:
160  // hack to detect the case where the user pressed the
161  // Cancel button
162  fRetStr[0] = 0;
163  fRetStr[1] = 0;
164  delete this;
165  break;
166  }
167  default:
168  break;
169  }
170  break;
171 
172  case kC_TEXTENTRY:
173  switch (GET_SUBMSG(msg)) {
174  case kTE_ENTER:
175  // here copy the string from text buffer to return variable
176  // coverity[secure_coding]
177  strcpy(fRetStr, fTE->GetBuffer()->GetString());
178  // if user selected an empty string, set the second
179  // char to 1,in order to distinguish between empty string
180  // selected with OK and Cancel button pressed
181  if (!strcmp(fRetStr, ""))
182  fRetStr[1] = 1;
183  delete this;
184  break;
185  default:
186  break;
187  }
188  break;
189 
190  default:
191  break;
192  }
193  return kTRUE;
194 }
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
#define gClient
Definition: TGClient.h:166
bool Bool_t
Definition: RtypesCore.h:59
const char * GetString() const
Definition: TGTextBuffer.h:47
~TGInputDialog()
Cleanup dialog.
void AddText(Int_t pos, const char *text)
Definition: TGTextBuffer.h:49
TGTextBuffer * GetBuffer() const
Definition: TGTextEntry.h:127
Int_t GET_SUBMSG(Long_t val)
unsigned int UInt_t
Definition: RtypesCore.h:42
Int_t GET_MSG(Long_t val)
TGTextEntry * fTE
Definition: TGInputDialog.h:33
long Long_t
Definition: RtypesCore.h:50
#define ClassImp(name)
Definition: Rtypes.h:336
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1054
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
virtual Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t)
Handle button and text enter events.
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:200
void MakeZombie()
Definition: TObject.h:49
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:949
const Bool_t kTRUE
Definition: RtypesCore.h:91
int main(int argc, char **argv)